diff options
Diffstat (limited to 'internal/cvss/v2vector_test.go')
-rw-r--r-- | internal/cvss/v2vector_test.go | 100 |
1 files changed, 59 insertions, 41 deletions
diff --git a/internal/cvss/v2vector_test.go b/internal/cvss/v2vector_test.go index a943957..f297ba6 100644 --- a/internal/cvss/v2vector_test.go +++ b/internal/cvss/v2vector_test.go @@ -4,48 +4,66 @@ import ( "testing" ) -// Test cvss v2 -func TestCvssV2Parser(t *testing.T) { - // test v2 vector - test := "AV:L/AC:L/Au:N/C:N/I:N/A:P" - expMetrics := []string { "AV:L", "AC:L", "Au:N", "C:N", "I:N", "A:P" } - - t.Run("TestV2Parser", func(t *testing.T) { - // parse vector - vec, err := NewVector(test) - if err != nil { - t.Error(err) - return - } - - // check version - if vec.Version() != V20 { - t.Errorf("got %s, exp %s", vec.Version(), V20) - return - } - - for i, m := range(vec.Metrics()) { - got := m.String() - if got != expMetrics[i] { - t.Errorf("got %s, exp %s", got, expMetrics[i]) +func TestNewV2Vector(t *testing.T) { + passTests := []struct { + val string + exp []string + } { + { + val: "AV:L/AC:L/Au:N/C:N/I:N/A:P", + exp: []string { "AV:L", "AC:L", "Au:N", "C:N", "I:N", "A:P" }, + }, + } + + for _, test := range(passTests) { + t.Run(test.val, func(t *testing.T) { + // parse vector + vec, err := newV2Vector(test.val) + if err != nil { + t.Error(err) + return + } + + // check version + if vec.Version() != V20 { + t.Errorf("got %s, exp %s", vec.Version(), V20) + return + } + + // check metric length + if len(vec.Metrics()) != len(test.exp) { + t.Errorf("got %d, exp %d", len(vec.Metrics()), len(test.exp)) + return } - } - }) -} -// Test cvss v2 -func TestInvalidV2Vector(t *testing.T) { - // test invalid vector - test := "AV:L/junk/Au:N/C:N/I:N/A:P" - exp := "invalid CVSS 2.0 metric: junk" - - // parse vector - got, err := NewVector(test) - if err != nil && err.Error() != exp { - t.Errorf("got \"%s\", exp \"%s\"", err.Error(), err) - return - } else if err == nil { - t.Errorf("got \"%s\", exp badMetric", got) - return + // check metrics + for i, m := range(vec.Metrics()) { + got := m.String() + if got != test.exp[i] { + t.Errorf("got %s, exp %s", got, test.exp[i]) + } + } + }) + } + + failTests := []struct { + val string + exp string + } { + { + val: "AV:L/junk/Au:N/C:N/I:N/A:P", + exp: "invalid CVSS 2.0 metric: junk", + }, + } + + for _, test := range(failTests) { + t.Run(test.val, func(t *testing.T) { + got, err := newV2Vector(test.val) + if err != nil && err.Error() != test.exp { + t.Errorf("got \"%s\", exp \"%s\"", err.Error(), test.exp) + } else if err == nil { + t.Errorf("got \"%s\", exp badMetric", got) + } + }) } } |