diff options
Diffstat (limited to 'internal')
-rw-r--r-- | internal/feed/feed.go | 41 | ||||
-rw-r--r-- | internal/feed/v2authentication.go | 39 | ||||
-rw-r--r-- | internal/feed/v2authentication_string.go | 24 | ||||
-rw-r--r-- | internal/feed/v2authentication_test.go | 77 |
4 files changed, 143 insertions, 38 deletions
diff --git a/internal/feed/feed.go b/internal/feed/feed.go index 56fc7d2..affa5d2 100644 --- a/internal/feed/feed.go +++ b/internal/feed/feed.go @@ -6,47 +6,12 @@ import ( ) const ( - None = iota // no priv req/user interaction - Low // low complexity/priv req - Medium // medium complexity/priv req - High // high complexity/priv req - - Critical // critical severity - - CvssVersion31 // CVSS version 3.1 - CvssVersion20 // CVSS version 2.0 - - Single // Single authentication + CvssVersion31 = iota // CVSS version 3.1 + CvssVersion20 // CVSS version 2.0 ) // TODO: parse cpe, cve id -// CVSS V2 authentication -type Authentication int - -// Unmarshal CVSS V2 authentication from JSON. -func (me *Authentication) 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 "NONE": - *me = None - case "SINGLE": - *me = Single - default: - // return error - return fmt.Errorf("unknown authentication: %s", s) - } - - // return success - return nil -} - // CVE metadata type CveMetadata struct { // CVE ID @@ -267,7 +232,7 @@ type CvssV2 struct { AccessComplexity V2AccessComplexity `json:"accessComplexity"` // authentication - Authentication Authentication `json:"authentication"` + Authentication V2Authentication `json:"authentication"` ConfidentialityImpact V2Impact `json:"confidentialityImpact"` IntegrityImpact V2Impact `json:"integrityImpact"` diff --git a/internal/feed/v2authentication.go b/internal/feed/v2authentication.go new file mode 100644 index 0000000..853954f --- /dev/null +++ b/internal/feed/v2authentication.go @@ -0,0 +1,39 @@ +package feed + +//go:generate stringer -linecomment -type=V2Authentication + +import ( + "encoding/json" + "fmt" +) + +// CVSS v2 authentication +type V2Authentication byte + +const ( + V2AuthNone V2Authentication = iota // NONE + V2AuthSingle // SINGLE +) + +// Unmarshal CVSS V2 authentication from JSON. +func (me *V2Authentication) 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 "NONE": + *me = V2AuthNone + case "SINGLE": + *me = V2AuthSingle + default: + // return error + return fmt.Errorf("unknown CVSS v2 authentication: %s", s) + } + + // return success + return nil +} diff --git a/internal/feed/v2authentication_string.go b/internal/feed/v2authentication_string.go new file mode 100644 index 0000000..856c808 --- /dev/null +++ b/internal/feed/v2authentication_string.go @@ -0,0 +1,24 @@ +// Code generated by "stringer -linecomment -type=V2Authentication"; 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[V2AuthNone-0] + _ = x[V2AuthSingle-1] +} + +const _V2Authentication_name = "NONESINGLE" + +var _V2Authentication_index = [...]uint8{0, 4, 10} + +func (i V2Authentication) String() string { + if i >= V2Authentication(len(_V2Authentication_index)-1) { + return "V2Authentication(" + strconv.FormatInt(int64(i), 10) + ")" + } + return _V2Authentication_name[_V2Authentication_index[i]:_V2Authentication_index[i+1]] +} diff --git a/internal/feed/v2authentication_test.go b/internal/feed/v2authentication_test.go new file mode 100644 index 0000000..4f23764 --- /dev/null +++ b/internal/feed/v2authentication_test.go @@ -0,0 +1,77 @@ +package feed + +import ( + "encoding/json" + "testing" +) + +func TestV2AuthenticationUnmarshalInvalidData(t *testing.T) { + test := []byte(`{}`) + var val V2Authentication + + if err := json.Unmarshal(test, &val); err == nil { + t.Errorf("got \"%s\", exp error", val) + } +} + +func TestV2AuthenticationUnmarshalUnknown(t *testing.T) { + test := []byte(`"foo"`) + exp := "unknown CVSS v2 authentication: foo" + var val V2Authentication + + 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 TestV2AuthenticationUnmarshalValid(t *testing.T) { + tests := []struct { + val string + exp V2Authentication + } { + { "\"NONE\"", V2AuthNone }, + { "\"SINGLE\"", V2AuthSingle }, + } + + for _, test := range(tests) { + t.Run(test.val, func(t *testing.T) { + var got V2Authentication + 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 TestV2AuthenticationString(t *testing.T) { + tests := []struct { + val V2Authentication + exp string + } { + { V2AuthNone, "NONE" }, + { V2AuthSingle, "SINGLE" }, + + { V2Authentication(255), "V2Authentication(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) + } + }) + } +} |