aboutsummaryrefslogtreecommitdiff
path: root/internal
diff options
context:
space:
mode:
authorPaul Duncan <pabs@pablotron.org>2022-02-01 13:23:44 -0500
committerPaul Duncan <pabs@pablotron.org>2022-02-01 13:23:44 -0500
commita40213480929a93a78239053a40a6ee93547c063 (patch)
treeb848aac63f0695b18a2f58928c7ff4d2a7eb4184 /internal
parent465f427a04c2f10bc5163f6bceef13693f7e99e6 (diff)
downloadcvez-a40213480929a93a78239053a40a6ee93547c063.tar.bz2
cvez-a40213480929a93a78239053a40a6ee93547c063.zip
internal/feed: add score, score tests
Diffstat (limited to 'internal')
-rw-r--r--internal/feed/feed.go21
-rw-r--r--internal/feed/score.go34
-rw-r--r--internal/feed/score_test.go92
3 files changed, 126 insertions, 21 deletions
diff --git a/internal/feed/feed.go b/internal/feed/feed.go
index 9c27fb0..b8a3849 100644
--- a/internal/feed/feed.go
+++ b/internal/feed/feed.go
@@ -21,27 +21,6 @@ const (
// TODO: parse cpe
-// CVSS score
-type Score float32
-
-// Unmarshal CVSS score from JSON.
-func (me *Score) UnmarshalJSON(b []byte) error {
- // decode float, check for error
- var v float32
- if err := json.Unmarshal(b, &v); err != nil {
- return err
- }
-
- // check score
- if v < 0.0 || v > 10.0 {
- return fmt.Errorf("score out of bounds: %f", v)
- }
-
- // save result, return success
- *me = Score(v)
- return nil
-}
-
// CVSS severity
type Severity int
diff --git a/internal/feed/score.go b/internal/feed/score.go
new file mode 100644
index 0000000..4e7da57
--- /dev/null
+++ b/internal/feed/score.go
@@ -0,0 +1,34 @@
+package feed
+
+import (
+ "encoding/json"
+ "fmt"
+ "math"
+ "strconv"
+)
+
+// CVSS score
+type Score float32
+
+// Unmarshal CVSS score from JSON.
+func (me *Score) UnmarshalJSON(b []byte) error {
+ // decode float, check for error
+ var v float32
+ if err := json.Unmarshal(b, &v); err != nil {
+ return err
+ }
+
+ // check score
+ if v < 0.0 || v > 10.0 {
+ return fmt.Errorf("CVSS score out of bounds: %2.1f", v)
+ }
+
+ // save result, return success
+ *me = Score(v)
+ return nil
+}
+
+func (me Score) String() string {
+ val := math.Trunc(10.0 * float64(me)) / 10.0
+ return strconv.FormatFloat(val, 'f', 1, 64)
+}
diff --git a/internal/feed/score_test.go b/internal/feed/score_test.go
new file mode 100644
index 0000000..0bda1b2
--- /dev/null
+++ b/internal/feed/score_test.go
@@ -0,0 +1,92 @@
+package feed
+
+import (
+ "encoding/json"
+ "testing"
+)
+
+func TestScoreUnmarshalInvalidData(t *testing.T) {
+ test := []byte(`{}`)
+ var val Score
+
+ if err := json.Unmarshal(test, &val); err == nil {
+ t.Errorf("got \"%s\", exp error", val)
+ }
+}
+
+func TestScoreUnmarshalInvalidValues(t *testing.T) {
+ tests := []struct {
+ val string
+ exp string
+ } {
+ { `-100.0`, "CVSS score out of bounds: -100.0" },
+ { `-90.0`, "CVSS score out of bounds: -90.0" },
+ { `-9.3`, "CVSS score out of bounds: -9.3" },
+ { `-1`, "CVSS score out of bounds: -1.0" },
+ { `10.1`, "CVSS score out of bounds: 10.1" },
+ { `100.0`, "CVSS score out of bounds: 100.0" },
+ }
+
+ for _, test := range(tests) {
+ t.Run(test.val, func(t *testing.T) {
+ var got Score
+
+ if err := json.Unmarshal([]byte(test.val), &got); err == nil {
+ t.Errorf("got \"%s\", exp error", got)
+ } else if err.Error() != test.exp {
+ t.Errorf("got \"%s\", exp \"%s\"", err.Error(), test.exp)
+ }
+ })
+ }
+}
+
+func TestScoreUnmarshalValidValues(t *testing.T) {
+ tests := []struct {
+ val string
+ exp float32
+ } {
+ { `0.0`, 0.0 },
+ { `0.1`, 0.1 },
+ { `1.2`, 1.2 },
+ { `5.9`, 5.9 },
+ { `9.9`, 9.9 },
+ { `10.0`, 10.0 },
+ }
+
+ for _, test := range(tests) {
+ t.Run(test.val, func(t *testing.T) {
+ var got Score
+
+ if err := json.Unmarshal([]byte(test.val), &got); err != nil {
+ t.Error(err)
+ return
+ } else if float32(got) != test.exp {
+ t.Errorf("got \"%f\", exp \"%f\"", float32(got), test.exp)
+ }
+ })
+ }
+}
+
+func TestScoreString(t *testing.T) {
+ tests := []struct {
+ val float32
+ exp string
+ } {
+ { 0.0, "0.0" },
+ { 0.01, "0.0" },
+ { 0.09, "0.0" },
+ { 1.2222, "1.2" },
+ { 5.9, "5.9" },
+ { 9.99, "9.9" },
+ { 10.0, "10.0" },
+ }
+
+ for _, test := range(tests) {
+ t.Run(test.exp, func(t *testing.T) {
+ got := Score(test.val).String()
+ if got != test.exp {
+ t.Errorf("got \"%s\", exp \"%s\"", got, test.exp)
+ }
+ })
+ }
+}