aboutsummaryrefslogtreecommitdiff
path: root/cvss/score_test.go
blob: 5bff3279ff56148cd9132187900afd6f2fa86787 (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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package cvss

import (
  "strconv"
  "testing"
)

func TestNewScore(t *testing.T) {
  // pass tests
  for exp := 0; exp < 100; exp++ {
    val := float64(exp) / 10.0

    t.Run(strconv.FormatInt(int64(exp), 10), func(t *testing.T) {
      got, err := NewScore(val)
      if err != nil {
        t.Error(err)
      } else if int(got) != exp {
        t.Errorf("got %d, exp %d", int(got), exp)
      }
    })
  }

  // fail tests
  failTests := []float64 { -10.0, -0.1, 10.1, 100.0 }
  for _, val := range(failTests) {
    t.Run(strconv.FormatFloat(val, 'f', 2, 64), func(t *testing.T) {
      if got, err := NewScore(val); err == nil {
        t.Errorf("got %v, exp error", got)
      }
    })
  }
}

func TestScoreString(t *testing.T) {
  tests := []struct {
    val float64
    exp string
  } {
    { 0.0, "0.0" },
    { 1.0, "1.0" },
    { 1.1, "1.1" },
    { 1.2, "1.2" },
    { 2.0, "2.0" },
    { 7.5, "7.5" },
    { 10.0, "10.0" },
  }

  for _, test := range(tests) {
    t.Run(test.exp, func(t *testing.T) {
      if val, err := NewScore(test.val); err != nil {
        t.Error(err)
      } else if val.String() != test.exp {
        t.Errorf("got \"%s\", exp \"%s\"", val.String(), test.exp)
      }
    })
  }
}

func TestScoreFloat(t *testing.T) {
  tests := []struct {
    val float64
    exp float32
  } {
    { 0.0, float32(0.0) },
    { 1.0, float32(1.0) },
    { 1.1, float32(1.1) },
    { 1.2, float32(1.2) },
    { 2.0, float32(2.0) },
    { 7.5, float32(7.5) },
    { 7.5, float32(7.5) },
    { 10.0, float32(10.0) },

    // test weird cases
    { 7.59, float32(7.5) },
    { 8.11111111, float32(8.1) },
  }

  for _, test := range(tests) {
    t.Run(strconv.FormatFloat(test.val, 'f', 2, 64), func(t *testing.T) {
      s, err := NewScore(test.val)
      if err != nil {
        t.Error(err)
        return
      }

      got := s.Float()
      if got != test.exp {
        t.Errorf("got \"%f\", exp \"%f\"", got, test.exp)
      }
    })
  }
}

func TestScoreSeverity(t *testing.T) {
  passTests := []struct {
    val float64
    exp Severity
  } {
    { 0.0, None },
    { 0.1, Low },
    { 3.9, Low },
    { 4.0, Medium },
    { 6.9, Medium },
    { 7.0, High },
    { 8.9, High },
    { 9.0, Critical },
    { 10.0, Critical },
  }

  for _, test := range(passTests) {
    t.Run(strconv.FormatFloat(test.val, 'f', 2, 64), func(t *testing.T) {
      s, err := NewScore(test.val)
      if err != nil {
        t.Error(err)
        return
      }

      got := s.Severity()
      if got != test.exp {
        t.Errorf("got \"%s\", exp \"%s\"", got, test.exp)
      }
    })
  }

  failTests := []struct {
    val Score
    exp Severity
  } {
    { Score(uint8(110)), Unknown },
  }

  for _, test := range(failTests) {
    t.Run(test.val.String(), func(t *testing.T) {
      got := test.val.Severity()
      if got != test.exp {
        t.Errorf("got \"%s\", exp \"%s\"", got, test.exp)
      }
    })
  }
}