aboutsummaryrefslogtreecommitdiff
path: root/internal/feed/vector_test.go
blob: e956884f0eed2a8af2f3c1269c6e0ae6d428284c (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
package feed

import (
  "encoding/json"
  "testing"
)

func TestVectorUnmarshalInvalidData(t *testing.T) {
  test := []byte(`{}`)
  var val Vector

  if err := json.Unmarshal(test, &val); err == nil {
    t.Errorf("got \"%s\", exp error", val)
  }
}

func TestVectorUnmarshalJSON(t *testing.T) {
  failTests := []struct {
    val string
    exp string
  } {
    {
      val: "\"AV:N/junk/Au:S/C:P/I:P/A:P\"",
      exp: "invalid CVSS vector: AV:N/junk/Au:S/C:P/I:P/A:P",
    }, {
      val: "\"CVSS:3.0/junk/AC:H/PR:H/UI:R/S:U/C:H/I:H/A:H\"",
      exp: "invalid CVSS vector: CVSS:3.0/junk/AC:H/PR:H/UI:R/S:U/C:H/I:H/A:H",
    }, {
      val: "\"CVSS:3.1/AV:A/junk/PR:N/UI:N/S:U/C:H/I:H/A:H\"",
      exp: "invalid CVSS vector: CVSS:3.1/AV:A/junk/PR:N/UI:N/S:U/C:H/I:H/A:H",
    },
  }

  for _, test := range(failTests) {
    t.Run(test.val, func(t *testing.T) {
      var got Vector

      if err := json.Unmarshal([]byte(test.val), &got); err == nil {
        t.Errorf("got \"%v\", exp error", got)
      } else if err.Error() != test.exp {
        t.Errorf("got \"%s\", exp \"%s\"", err.Error(), test.exp)
      }
    })
  }

  passTests := []string {
    "AV:N/AC:M/Au:S/C:P/I:P/A:P",
    "CVSS:3.0/AV:A/AC:H/PR:H/UI:R/S:U/C:H/I:H/A:H",
    "CVSS:3.1/AV:A/AC:H/PR:N/UI:N/S:U/C:H/I:H/A:H",
  }

  for _, val := range(passTests) {
    t.Run(val, func(t *testing.T) {
      var got Vector

      if err := json.Unmarshal([]byte("\"" + val + "\""), &got); err != nil {
        t.Error(err)
      } else if got.Vector.String() != val {
        t.Errorf("got \"%s\", exp \"%s\"", got.Vector.String(), val)
      }
    })
  }
}