From f2cef211cda0d86ccd42e8aa411f9865a2a22cce Mon Sep 17 00:00:00 2001 From: Paul Duncan Date: Mon, 7 Feb 2022 11:07:39 -0500 Subject: cvss: rename v2Score to Score, update deps and tests --- cvss/score.go | 32 ++++++++++++++++++ cvss/score_test.go | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++++ cvss/v2score.go | 32 ------------------ cvss/v2score_test.go | 92 ---------------------------------------------------- cvss/v2scores.go | 18 +++++----- 5 files changed, 133 insertions(+), 133 deletions(-) create mode 100644 cvss/score.go create mode 100644 cvss/score_test.go delete mode 100644 cvss/v2score.go delete mode 100644 cvss/v2score_test.go (limited to 'cvss') diff --git a/cvss/score.go b/cvss/score.go new file mode 100644 index 0000000..e6a6faa --- /dev/null +++ b/cvss/score.go @@ -0,0 +1,32 @@ +package cvss + +import ( + "fmt" +) + +// Individual CVSS score. +// +// Note: since scores range from 0.0 to 10.0 with one decimal place of +// precision, they can be safely represented as a uint8. +type Score uint8 + +// Return floating point representation of score. +func NewScore(val float64) (Score, error) { + // check score range + if val < 0.0 || val > 10.0 { + return Score(0), fmt.Errorf("score value out of range [0, 10]: %f", val) + } + + // convert to score, return success + return Score(uint8(10.0 * val)), nil +} + +// Return string representation of score. +func (s Score) String() string { + return fmt.Sprintf("%d.%d", s / 10, s % 10) +} + +// Return floating point representation of score. +func (s Score) Float() float32 { + return float32(s) / 10.0 +} diff --git a/cvss/score_test.go b/cvss/score_test.go new file mode 100644 index 0000000..d35c4c8 --- /dev/null +++ b/cvss/score_test.go @@ -0,0 +1,92 @@ +package cvss + +import ( + "strconv" + "testing" +) + +func TestNewScore(t *testing.T) { + // pass tests + for exp := 0; exp < 100; exp++ { + val := float64(exp) / 10.0 + + t.Run(strconv.FormatInt(int64(exp), 10), func(t *testing.T) { + got, err := NewScore(val) + if err != nil { + t.Error(err) + } else if int(got) != exp { + t.Errorf("got %d, exp %d", int(got), exp) + } + }) + } + + // fail tests + failTests := []float64 { -10.0, -0.1, 10.1, 100.0 } + for _, val := range(failTests) { + t.Run(strconv.FormatFloat(val, 'f', 2, 64), func(t *testing.T) { + if got, err := NewScore(val); err == nil { + t.Errorf("got %v, exp error", got) + } + }) + } +} + +func TestScoreString(t *testing.T) { + tests := []struct { + val float64 + exp string + } { + { 0.0, "0.0" }, + { 1.0, "1.0" }, + { 1.1, "1.1" }, + { 1.2, "1.2" }, + { 2.0, "2.0" }, + { 7.5, "7.5" }, + { 10.0, "10.0" }, + } + + for _, test := range(tests) { + t.Run(test.exp, func(t *testing.T) { + if val, err := NewScore(test.val); err != nil { + t.Error(err) + } else if val.String() != test.exp { + t.Errorf("got \"%s\", exp \"%s\"", val.String(), test.exp) + } + }) + } +} + +func TestScoreFloat(t *testing.T) { + tests := []struct { + val float64 + exp float32 + } { + { 0.0, float32(0.0) }, + { 1.0, float32(1.0) }, + { 1.1, float32(1.1) }, + { 1.2, float32(1.2) }, + { 2.0, float32(2.0) }, + { 7.5, float32(7.5) }, + { 7.5, float32(7.5) }, + { 10.0, float32(10.0) }, + + // test weird cases + { 7.59, float32(7.5) }, + { 8.11111111, float32(8.1) }, + } + + for _, test := range(tests) { + t.Run(strconv.FormatFloat(test.val, 'f', 2, 64), func(t *testing.T) { + s, err := NewScore(test.val) + if err != nil { + t.Error(err) + return + } + + got := s.Float() + if got != test.exp { + t.Errorf("got \"%f\", exp \"%f\"", got, test.exp) + } + }) + } +} diff --git a/cvss/v2score.go b/cvss/v2score.go deleted file mode 100644 index 5740c09..0000000 --- a/cvss/v2score.go +++ /dev/null @@ -1,32 +0,0 @@ -package cvss - -import ( - "fmt" -) - -// Individual CVSS v2 score. -// -// Note: since scores range from 0.0 to 10.0 with one decimal place of -// precision, they can be safely represented as a uint8. -type v2Score uint8 - -// Return floating point representation of score. -func newV2Score(val float64) (v2Score, error) { - // check score range - if val < 0.0 || val > 10.0 { - return v2Score(0), fmt.Errorf("score value out of range [0, 10]: %f", val) - } - - // convert to score, return success - return v2Score(uint8(10.0 * val)), nil -} - -// Return string representation of score. -func (s v2Score) String() string { - return fmt.Sprintf("%d.%d", s / 10, s % 10) -} - -// Return floating point representation of score. -func (s v2Score) Float() float32 { - return float32(s) / 10.0 -} diff --git a/cvss/v2score_test.go b/cvss/v2score_test.go deleted file mode 100644 index f83b76e..0000000 --- a/cvss/v2score_test.go +++ /dev/null @@ -1,92 +0,0 @@ -package cvss - -import ( - "strconv" - "testing" -) - -func TestNewV2Score(t *testing.T) { - // pass tests - for exp := 0; exp < 100; exp++ { - val := float64(exp) / 10.0 - - t.Run(strconv.FormatInt(int64(exp), 10), func(t *testing.T) { - got, err := newV2Score(val) - if err != nil { - t.Error(err) - } else if int(got) != exp { - t.Errorf("got %d, exp %d", int(got), exp) - } - }) - } - - // fail tests - failTests := []float64 { -10.0, -0.1, 10.1, 100.0 } - for _, val := range(failTests) { - t.Run(strconv.FormatFloat(val, 'f', 2, 64), func(t *testing.T) { - if got, err := newV2Score(val); err == nil { - t.Errorf("got %v, exp error", got) - } - }) - } -} - -func TestV2ScoreString(t *testing.T) { - tests := []struct { - val float64 - exp string - } { - { 0.0, "0.0" }, - { 1.0, "1.0" }, - { 1.1, "1.1" }, - { 1.2, "1.2" }, - { 2.0, "2.0" }, - { 7.5, "7.5" }, - { 10.0, "10.0" }, - } - - for _, test := range(tests) { - t.Run(test.exp, func(t *testing.T) { - if val, err := newV2Score(test.val); err != nil { - t.Error(err) - } else if val.String() != test.exp { - t.Errorf("got \"%s\", exp \"%s\"", val.String(), test.exp) - } - }) - } -} - -func TestV2ScoreFloat(t *testing.T) { - tests := []struct { - val float64 - exp float32 - } { - { 0.0, float32(0.0) }, - { 1.0, float32(1.0) }, - { 1.1, float32(1.1) }, - { 1.2, float32(1.2) }, - { 2.0, float32(2.0) }, - { 7.5, float32(7.5) }, - { 7.5, float32(7.5) }, - { 10.0, float32(10.0) }, - - // test weird cases - { 7.59, float32(7.5) }, - { 8.11111111, float32(8.1) }, - } - - for _, test := range(tests) { - t.Run(strconv.FormatFloat(test.val, 'f', 2, 64), func(t *testing.T) { - s, err := newV2Score(test.val) - if err != nil { - t.Error(err) - return - } - - got := s.Float() - if got != test.exp { - t.Errorf("got \"%f\", exp \"%f\"", got, test.exp) - } - }) - } -} diff --git a/cvss/v2scores.go b/cvss/v2scores.go index 03bbc24..990b805 100644 --- a/cvss/v2scores.go +++ b/cvss/v2scores.go @@ -6,27 +6,27 @@ import ( // CVSS v2 base, temporal, and environmental scores. type v2Scores struct { - base v2Score // base score - temporal v2Score // temporal score - env v2Score // environmental score + base Score // base score + temporal Score // temporal score + env Score // environmental score } // Create new CVSS v2Scores from floats. func newV2ScoresFromFloats(base, temporal, env float64) (v2Scores, error) { - // convert base score from float to v2score - baseScore, err := newV2Score(base) + // convert base from float to Score + baseScore, err := NewScore(base) if err != nil { return v2Scores{}, err } - // convert temporal score from float to v2score - tempScore, err := newV2Score(temporal) + // convert temporal from float to Score + tempScore, err := NewScore(temporal) if err != nil { return v2Scores{}, err } - // convert env score from float to v2score - envScore, err := newV2Score(env) + // convert env from float to Score + envScore, err := NewScore(env) if err != nil { return v2Scores{}, err } -- cgit v1.2.3