From abcd9428d9eb6cc8ac0189e6443930a67ebcabda Mon Sep 17 00:00:00 2001 From: Paul Duncan Date: Mon, 31 Jan 2022 22:07:06 -0500 Subject: internal/cvss: add many tests --- internal/cvss/v31vector_test.go | 52 ++++++++++++++++++++++++++++++++--------- 1 file changed, 41 insertions(+), 11 deletions(-) (limited to 'internal/cvss/v31vector_test.go') diff --git a/internal/cvss/v31vector_test.go b/internal/cvss/v31vector_test.go index 8340123..ba82edb 100644 --- a/internal/cvss/v31vector_test.go +++ b/internal/cvss/v31vector_test.go @@ -1,19 +1,49 @@ package cvss -import "testing" +import ( + "testing" +) -// test cvss v3.1 vector -var testCvssV3 = "CVSS:3.1/AV:L/AC:L/PR:H/UI:N/S:U/C:N/I:N/A:H" +func TestV31Vector(t *testing.T) { + // test v3 vector + test := "CVSS:3.1/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" } -// Test CVSS v3 parser. -func TestCvssV3Parser(t *testing.T) { - t.Run("TestV3", func(t *testing.T) { - // parse vector, check for error - if _, err := NewVector(testCvssV3); err != nil { - t.Fatal(err) + t.Run("TestV31Vector", func(t *testing.T) { + // parse vector + vec, err := NewVector(test) + if err != nil { + t.Error(err) + return + } + + // check version + if vec.Version() != V31 { + t.Errorf("got %s, exp %s", vec.Version(), V31) + return + } + + for i, m := range(vec.Metrics()) { + got := m.String() + if got != expMetrics[i] { + t.Errorf("got %s, exp %s", got, expMetrics[i]) + } } }) +} + +func TestInvalidV31Vector(t *testing.T) { + // test invalid vector + test := "CVSS:3.1/AV:L/junk/PR:L/UI:N/S:U/C:H/I:N/A:N" + exp := "invalid CVSS 3.1 metric: junk" - // fmt.Println(v.Metrics()) - // fmt.Println(v) + // 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 + } } -- cgit v1.2.3