aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Duncan <pabs@pablotron.org>2022-02-01 08:14:16 -0500
committerPaul Duncan <pabs@pablotron.org>2022-02-01 08:14:16 -0500
commita308b1046b3001950945abe35b20b284865958e7 (patch)
tree9ad9f4be5540619fd48ebed170cb09a8374a6291
parent5dbf57134000f5ff2c5c12342ac4439b6d395603 (diff)
downloadcvez-a308b1046b3001950945abe35b20b284865958e7.tar.bz2
cvez-a308b1046b3001950945abe35b20b284865958e7.zip
internal/feed: add v3attackvector tests
-rw-r--r--internal/feed/feed.go60
-rw-r--r--internal/feed/v3attackvector.go46
-rw-r--r--internal/feed/v3attackvector_string.go26
-rw-r--r--internal/feed/v3attackvector_test.go81
4 files changed, 168 insertions, 45 deletions
diff --git a/internal/feed/feed.go b/internal/feed/feed.go
index 93a4a8f..a04c3ce 100644
--- a/internal/feed/feed.go
+++ b/internal/feed/feed.go
@@ -3,16 +3,10 @@ package feed
import (
"encoding/json"
"fmt"
- // "strconv"
)
const (
- AdjacentNetwork = iota // Adjacent Network attack vector.
- Network // Network attack vector.
- Local // Local attack vector.
- Physical // Physical attack vector.
-
- None // no priv req/user interaction
+ None = iota // no priv req/user interaction
Low // low complexity/priv req
Medium // medium complexity/priv req
High // high complexity/priv req
@@ -35,36 +29,6 @@ const (
// TODO: parse cpe
-// CVSS attack vector
-type AttackVector int
-
-// Unmarshal CVSS attack vector from JSON.
-func (me *AttackVector) 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 "ADJACENT_NETWORK":
- *me = AdjacentNetwork
- case "LOCAL":
- *me = Local
- case "NETWORK":
- *me = Network
- case "PHYSICAL":
- *me = Physical
- default:
- // return error
- return fmt.Errorf("unknown attack vector: %s", s)
- }
-
- // return success
- return nil
-}
-
// CVSS attack complexity
type AttackComplexity int
@@ -258,10 +222,16 @@ func (me *Severity) UnmarshalJSON(b []byte) error {
return nil
}
-type AccessVector int
+type V2AccessVector byte
+
+const (
+ V2AVAdjacentNetwork V2AccessVector = iota
+ V2AVLocal
+ V2AVNetwork
+)
// Unmarshal CVSS V2 access vector from JSON.
-func (me *AccessVector) UnmarshalJSON(b []byte) error {
+func (me *V2AccessVector) UnmarshalJSON(b []byte) error {
// decode string, check for error
var s string
if err := json.Unmarshal(b, &s); err != nil {
@@ -271,14 +241,14 @@ func (me *AccessVector) UnmarshalJSON(b []byte) error {
// check value
switch s {
case "ADJACENT_NETWORK":
- *me = AdjacentNetwork
+ *me = V2AVAdjacentNetwork
case "LOCAL":
- *me = Local
+ *me = V2AVLocal
case "NETWORK":
- *me = Network
+ *me = V2AVNetwork
default:
// return error
- return fmt.Errorf("unknown CVSS access vector: %s", s)
+ return fmt.Errorf("unknown CVSS v2 access vector: %s", s)
}
// return success
@@ -511,7 +481,7 @@ type CvssV3 struct {
VectorString string `json:"vectorString"`
// attack vector
- AttackVector AttackVector `json:"attackVector"`
+ AttackVector V3AttackVector `json:"attackVector"`
// attack complexity
AttackComplexity AttackComplexity `json:"attackComplexity"`
@@ -554,7 +524,7 @@ type CvssV2 struct {
VectorString string `json:"vectorString"`
// attack vector
- AccessVector AccessVector `json:"accessVector"`
+ AccessVector V2AccessVector `json:"accessVector"`
// attack complexity
AccessComplexity AccessComplexity `json:"accessComplexity"`
diff --git a/internal/feed/v3attackvector.go b/internal/feed/v3attackvector.go
new file mode 100644
index 0000000..ecc309a
--- /dev/null
+++ b/internal/feed/v3attackvector.go
@@ -0,0 +1,46 @@
+package feed
+
+//go:generate stringer -linecomment -type=V3AttackVector
+
+import (
+ "encoding/json"
+ "fmt"
+)
+
+// CVSS v3 attack vector.
+type V3AttackVector byte
+
+const (
+ V3AVAdjacentNetwork V3AttackVector = iota // ADJACENT_NETWORK
+ V3AVNetwork // NETWORK
+ V3AVLocal // LOCAL
+ V3AVPhysical // PHYSICAL
+)
+
+// Unmarshal CVSS v3 attack vector from JSON.
+func (me *V3AttackVector) 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 "ADJACENT_NETWORK":
+ *me = V3AVAdjacentNetwork
+ case "LOCAL":
+ *me = V3AVLocal
+ case "NETWORK":
+ *me = V3AVNetwork
+ case "PHYSICAL":
+ *me = V3AVPhysical
+ default:
+ // return error
+ return fmt.Errorf("unknown CVSS v3 attack vector: %s", s)
+ }
+
+ // return success
+ return nil
+}
+
diff --git a/internal/feed/v3attackvector_string.go b/internal/feed/v3attackvector_string.go
new file mode 100644
index 0000000..277520f
--- /dev/null
+++ b/internal/feed/v3attackvector_string.go
@@ -0,0 +1,26 @@
+// Code generated by "stringer -linecomment -type=V3AttackVector"; 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[V3AVAdjacentNetwork-0]
+ _ = x[V3AVNetwork-1]
+ _ = x[V3AVLocal-2]
+ _ = x[V3AVPhysical-3]
+}
+
+const _V3AttackVector_name = "ADJACENT_NETWORKNETWORKLOCALPHYSICAL"
+
+var _V3AttackVector_index = [...]uint8{0, 16, 23, 28, 36}
+
+func (i V3AttackVector) String() string {
+ if i >= V3AttackVector(len(_V3AttackVector_index)-1) {
+ return "V3AttackVector(" + strconv.FormatInt(int64(i), 10) + ")"
+ }
+ return _V3AttackVector_name[_V3AttackVector_index[i]:_V3AttackVector_index[i+1]]
+}
diff --git a/internal/feed/v3attackvector_test.go b/internal/feed/v3attackvector_test.go
new file mode 100644
index 0000000..251cfd4
--- /dev/null
+++ b/internal/feed/v3attackvector_test.go
@@ -0,0 +1,81 @@
+package feed
+
+import (
+ "encoding/json"
+ "testing"
+)
+
+func TestV3AttackVectorUnmarshalInvalidData(t *testing.T) {
+ test := []byte(`{}`)
+ var val V3AttackVector
+
+ if err := json.Unmarshal(test, &val); err == nil {
+ t.Errorf("got \"%s\", exp error", val)
+ }
+}
+
+func TestV3AttackVectorUnmarshalUnknown(t *testing.T) {
+ test := []byte(`"foo"`)
+ exp := "unknown CVSS v3 attack vector: foo"
+ var val V3AttackVector
+
+ 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 TestV3AttackVectorUnmarshalValid(t *testing.T) {
+ tests := []struct {
+ val string
+ exp V3AttackVector
+ } {
+ { "\"ADJACENT_NETWORK\"", V3AVAdjacentNetwork },
+ { "\"LOCAL\"", V3AVLocal },
+ { "\"NETWORK\"", V3AVNetwork },
+ { "\"PHYSICAL\"", V3AVPhysical },
+ }
+
+ for _, test := range(tests) {
+ t.Run(test.val, func(t *testing.T) {
+ var got V3AttackVector
+ 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 TestV3AttackVectorString(t *testing.T) {
+ tests := []struct {
+ val V3AttackVector
+ exp string
+ } {
+ { V3AVAdjacentNetwork, "ADJACENT_NETWORK" },
+ { V3AVLocal, "LOCAL" },
+ { V3AVNetwork, "NETWORK" },
+ { V3AVPhysical, "PHYSICAL" },
+
+ { V3AttackVector(255), "V3AttackVector(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)
+ }
+ })
+ }
+}