diff options
Diffstat (limited to 'internal/cvss/v30vector_test.go')
-rw-r--r-- | internal/cvss/v30vector_test.go | 98 |
1 files changed, 59 insertions, 39 deletions
diff --git a/internal/cvss/v30vector_test.go b/internal/cvss/v30vector_test.go index fae3436..1d44d35 100644 --- a/internal/cvss/v30vector_test.go +++ b/internal/cvss/v30vector_test.go @@ -4,46 +4,66 @@ import ( "testing" ) -func TestV30Vector(t *testing.T) { - // test v3 vector - test := "CVSS:3.0/AV:L/AC:H/PR:L/UI:N/S:U/C:H/I:N/A:N" - expMetrics := []string { "AV:L", "AC:H", "PR:L", "UI:N", "S:U", "C:H", "I:N", "A:N" } - - t.Run("TestV30Vector", func(t *testing.T) { - // parse vector - vec, err := NewVector(test) - if err != nil { - t.Error(err) - return - } - - // check version - if vec.Version() != V30 { - t.Errorf("got %s, exp %s", vec.Version(), V30) - return - } - - for i, m := range(vec.Metrics()) { - got := m.String() - if got != expMetrics[i] { - t.Errorf("got %s, exp %s", got, expMetrics[i]) +func TestNewV30Vector(t *testing.T) { + passTests := []struct { + val string + exp []string + } { + { + val: "CVSS:3.0/AV:L/AC:H/PR:L/UI:N/S:U/C:H/I:N/A:N", + exp: []string { "AV:L", "AC:H", "PR:L", "UI:N", "S:U", "C:H", "I:N", "A:N" }, + }, + } + + for _, test := range(passTests) { + t.Run(test.val, func(t *testing.T) { + // parse vector + vec, err := newV30Vector(test.val) + if err != nil { + t.Error(err) + return + } + + // check version + if vec.Version() != V30 { + t.Errorf("got %s, exp %s", vec.Version(), V30) + return + } + + // check metric length + if len(vec.Metrics()) != len(test.exp) { + t.Errorf("got %d, exp %d", len(vec.Metrics()), len(test.exp)) + return } - } - }) -} -func TestInvalidV30Vector(t *testing.T) { - // test invalid vector - test := "CVSS:3.0/AV:L/junk/PR:L/UI:N/S:U/C:H/I:N/A:N" - exp := "invalid CVSS 3.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: "CVSS:3.0/AV:L/junk/PR:L/UI:N/S:U/C:H/I:N/A:N", + exp: "invalid CVSS 3.0 metric: junk", + }, + } + + for _, test := range(failTests) { + t.Run(test.val, func(t *testing.T) { + got, err := newV30Vector(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) + } + }) } } |