diff options
| author | Paul Duncan <pabs@pablotron.org> | 2022-02-01 12:31:05 -0500 | 
|---|---|---|
| committer | Paul Duncan <pabs@pablotron.org> | 2022-02-01 12:31:05 -0500 | 
| commit | f4f3bf7f03e790b5d98004de3498c102bdac50ad (patch) | |
| tree | a856870ae1f33c89715d7930a0908b04e0aa47d2 | |
| parent | 00206f4dfbb55cc56d24bdfd9f015e912120c72c (diff) | |
| download | cvez-f4f3bf7f03e790b5d98004de3498c102bdac50ad.tar.xz cvez-f4f3bf7f03e790b5d98004de3498c102bdac50ad.zip  | |
internal/feed: add v3scope tests
| -rw-r--r-- | internal/feed/feed.go | 33 | ||||
| -rw-r--r-- | internal/feed/v3scope.go | 39 | ||||
| -rw-r--r-- | internal/feed/v3scope_string.go | 24 | ||||
| -rw-r--r-- | internal/feed/v3scope_test.go | 77 | 
4 files changed, 141 insertions, 32 deletions
diff --git a/internal/feed/feed.go b/internal/feed/feed.go index 98e62ef..b9f7678 100644 --- a/internal/feed/feed.go +++ b/internal/feed/feed.go @@ -11,11 +11,6 @@ const (    Medium          // medium complexity/priv req    High            // high complexity/priv req -  Required        // user interaction required - -  Changed         // scope changed -  Unchanged       // scope unchanged -    Complete        // complete integrity impact    Partial         // partial integrity impact @@ -29,32 +24,6 @@ const (  // TODO: parse cpe -// CVSS scope -type Scope int - -// Unmarshal CVSS scope from JSON. -func (me *Scope) 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 "CHANGED": -    *me = Changed -  case "UNCHANGED": -    *me = Unchanged -  default: -    // return error -    return fmt.Errorf("unknown scope: %s", s) -  } - -  // return success -  return nil -} -  // CVSS integrity/availability impact level  type ImpactLevel int @@ -409,7 +378,7 @@ type CvssV3 struct {    UserInteraction V3UserInteraction `json:"userInteraction"`    // scope -  Scope Scope `json:"scope"` +  Scope V3Scope `json:"scope"`    // integrity impact    IntegrityImpact ImpactLevel `json:"integrityImpact"` diff --git a/internal/feed/v3scope.go b/internal/feed/v3scope.go new file mode 100644 index 0000000..20fe0a5 --- /dev/null +++ b/internal/feed/v3scope.go @@ -0,0 +1,39 @@ +package feed + +//go:generate stringer -linecomment -type=V3Scope + +import ( +  "encoding/json" +  "fmt" +) + +// CVSS v3 scope. +type V3Scope byte + +const ( +  V3ScopeChanged V3Scope = iota // CHANGED +  V3ScopeUnchanged // UNCHANGED +) + +// Unmarshal CVSS scope from JSON. +func (me *V3Scope) 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 "CHANGED": +    *me = V3ScopeChanged +  case "UNCHANGED": +    *me = V3ScopeUnchanged +  default: +    // return error +    return fmt.Errorf("unknown CVSS v3 scope: %s", s) +  } + +  // return success +  return nil +} diff --git a/internal/feed/v3scope_string.go b/internal/feed/v3scope_string.go new file mode 100644 index 0000000..982cead --- /dev/null +++ b/internal/feed/v3scope_string.go @@ -0,0 +1,24 @@ +// Code generated by "stringer -linecomment -type=V3Scope"; 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[V3ScopeChanged-0] +	_ = x[V3ScopeUnchanged-1] +} + +const _V3Scope_name = "CHANGEDUNCHANGED" + +var _V3Scope_index = [...]uint8{0, 7, 16} + +func (i V3Scope) String() string { +	if i >= V3Scope(len(_V3Scope_index)-1) { +		return "V3Scope(" + strconv.FormatInt(int64(i), 10) + ")" +	} +	return _V3Scope_name[_V3Scope_index[i]:_V3Scope_index[i+1]] +} diff --git a/internal/feed/v3scope_test.go b/internal/feed/v3scope_test.go new file mode 100644 index 0000000..54170b0 --- /dev/null +++ b/internal/feed/v3scope_test.go @@ -0,0 +1,77 @@ +package feed + +import ( +  "encoding/json" +  "testing" +) + +func TestV3ScopeUnmarshalInvalidData(t *testing.T) { +  test := []byte(`{}`) +  var val V3Scope + +  if err := json.Unmarshal(test, &val); err == nil { +    t.Errorf("got \"%s\", exp error", val) +  } +} + +func TestV3ScopeUnmarshalUnknown(t *testing.T) { +  test := []byte(`"foo"`) +  exp := "unknown CVSS v3 scope: foo" +  var val V3Scope + +  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 TestV3ScopeUnmarshalValid(t *testing.T) { +  tests := []struct { +    val string +    exp V3Scope +  } { +    { "\"CHANGED\"", V3ScopeChanged }, +    { "\"UNCHANGED\"", V3ScopeUnchanged }, +  } + +  for _, test := range(tests) { +    t.Run(test.val, func(t *testing.T) { +      var got V3Scope +      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 TestV3ScopeString(t *testing.T) { +  tests := []struct { +    val V3Scope +    exp string +  } { +    { V3ScopeChanged, "CHANGED" }, +    { V3ScopeUnchanged, "UNCHANGED" }, + +    { V3Scope(255), "V3Scope(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) +      } +    }) +  } +}  | 
