aboutsummaryrefslogtreecommitdiff
path: root/internal/cvss/v2vector_test.go
blob: f297ba6aac7fe0a0dcaed5843eabfdf98f86e1bd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package cvss

import (
  "testing"
)

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
      }

      // 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)
      }
    })
  }
}