From 6f889af4b0d99ee714bfe33e7f864bc35f567097 Mon Sep 17 00:00:00 2001 From: Paul Duncan Date: Tue, 1 Feb 2022 14:24:40 -0500 Subject: internal/feed: add v2accesscomplexity, tests --- internal/feed/feed.go | 33 +------------ internal/feed/v2accesscomplexity.go | 42 ++++++++++++++++ internal/feed/v2accesscomplexity_string.go | 25 ++++++++++ internal/feed/v2accesscomplexity_test.go | 79 ++++++++++++++++++++++++++++++ 4 files changed, 148 insertions(+), 31 deletions(-) create mode 100644 internal/feed/v2accesscomplexity.go create mode 100644 internal/feed/v2accesscomplexity_string.go create mode 100644 internal/feed/v2accesscomplexity_test.go (limited to 'internal/feed') diff --git a/internal/feed/feed.go b/internal/feed/feed.go index aa5fd08..56fc7d2 100644 --- a/internal/feed/feed.go +++ b/internal/feed/feed.go @@ -19,36 +19,7 @@ const ( Single // Single authentication ) -// TODO: parse cpe - -// CVSS V2 attack complexity -type AccessComplexity int - -// Unmarshal CVSS V2 access complexity from JSON. -func (me *AccessComplexity) 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 access complexity: %s", s) - } - - // return success - return nil -} - +// TODO: parse cpe, cve id // CVSS V2 authentication type Authentication int @@ -293,7 +264,7 @@ type CvssV2 struct { AccessVector V2AccessVector `json:"accessVector"` // attack complexity - AccessComplexity AccessComplexity `json:"accessComplexity"` + AccessComplexity V2AccessComplexity `json:"accessComplexity"` // authentication Authentication Authentication `json:"authentication"` diff --git a/internal/feed/v2accesscomplexity.go b/internal/feed/v2accesscomplexity.go new file mode 100644 index 0000000..5885e0d --- /dev/null +++ b/internal/feed/v2accesscomplexity.go @@ -0,0 +1,42 @@ +package feed + +//go:generate stringer -linecomment -type=V2AccessComplexity + +import ( + "encoding/json" + "fmt" +) + +// CVSS v2 access complexity +type V2AccessComplexity byte + +const ( + V2ACLow V2AccessComplexity = iota // LOW + V2ACMedium // MEDIUM + V2ACHigh // HIGH +) + +// Unmarshal CVSS V2 access complexity from JSON. +func (me *V2AccessComplexity) 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 = V2ACLow + case "MEDIUM": + *me = V2ACMedium + case "HIGH": + *me = V2ACHigh + default: + // return error + return fmt.Errorf("unknown CVSS v2 access complexity: %s", s) + } + + // return success + return nil +} diff --git a/internal/feed/v2accesscomplexity_string.go b/internal/feed/v2accesscomplexity_string.go new file mode 100644 index 0000000..8638b3d --- /dev/null +++ b/internal/feed/v2accesscomplexity_string.go @@ -0,0 +1,25 @@ +// Code generated by "stringer -linecomment -type=V2AccessComplexity"; 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[V2ACLow-0] + _ = x[V2ACMedium-1] + _ = x[V2ACHigh-2] +} + +const _V2AccessComplexity_name = "LOWMEDIUMHIGH" + +var _V2AccessComplexity_index = [...]uint8{0, 3, 9, 13} + +func (i V2AccessComplexity) String() string { + if i >= V2AccessComplexity(len(_V2AccessComplexity_index)-1) { + return "V2AccessComplexity(" + strconv.FormatInt(int64(i), 10) + ")" + } + return _V2AccessComplexity_name[_V2AccessComplexity_index[i]:_V2AccessComplexity_index[i+1]] +} diff --git a/internal/feed/v2accesscomplexity_test.go b/internal/feed/v2accesscomplexity_test.go new file mode 100644 index 0000000..2dd173d --- /dev/null +++ b/internal/feed/v2accesscomplexity_test.go @@ -0,0 +1,79 @@ +package feed + +import ( + "encoding/json" + "testing" +) + +func TestV2AccessComplexityUnmarshalInvalidData(t *testing.T) { + test := []byte(`{}`) + var val V2AccessComplexity + + if err := json.Unmarshal(test, &val); err == nil { + t.Errorf("got \"%s\", exp error", val) + } +} + +func TestV2AccessComplexityUnmarshalUnknown(t *testing.T) { + test := []byte(`"foo"`) + exp := "unknown CVSS v2 access complexity: foo" + var val V2AccessComplexity + + 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 TestV2AccessComplexityUnmarshalValid(t *testing.T) { + tests := []struct { + val string + exp V2AccessComplexity + } { + { "\"LOW\"", V2ACLow }, + { "\"MEDIUM\"", V2ACMedium }, + { "\"HIGH\"", V2ACHigh }, + } + + for _, test := range(tests) { + t.Run(test.val, func(t *testing.T) { + var got V2AccessComplexity + 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 TestV2AccessComplexityString(t *testing.T) { + tests := []struct { + val V2AccessComplexity + exp string + } { + { V2ACLow, "LOW" }, + { V2ACMedium, "MEDIUM" }, + { V2ACHigh, "HIGH" }, + + { V2AccessComplexity(255), "V2AccessComplexity(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) + } + }) + } +} -- cgit v1.2.3