aboutsummaryrefslogtreecommitdiff
path: root/internal/cpe/binding_test.go
blob: 7c5154ac902530d452645cbe5b42e7bd4fb1d980 (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
package cpe

import (
  "encoding/json"
  "testing"
)

func TestNewBinding(t *testing.T) {
  passTests := []string {
    "cpe:2.3:o:intel:ethernet_controller_e810_firmware:*:*:*:*:*:*:*:*",
  }

  for _, val := range(passTests) {
    t.Run(val, func(t *testing.T) {
      if _, err := NewBinding(val); err != nil {
        t.Error(err)
      }
    })
  }

  failTests := []struct {
    val string
    exp string
  } {{
    val: "o:intel:ethernet_controller_e810_firmware:*:*:*:*:*:*:*:*",
    exp: "missing CPE 2.3 prefix",
  }, {
    val: "cpe:2.3:\n",
    exp: "invalid byte: 0x0a",
  }, {
    val: "cpe:2.3:o:intel:ethernet_controller_e810_firmware:*:*:*:*:*:*:*",
    exp: "invalid attribute count: 10 != 11",
  }, {
    val: "cpe:2.3:foo:intel:ethernet_controller_e810_firmware:*:*:*:*:*:*:*:*",
    exp: "unknown part: \"foo\"",
  }}

  for _, test := range(failTests) {
    t.Run(test.val, func(t *testing.T) {
      got, err := NewBinding(test.val)
      if 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)
      }
    })
  }
}

func TestBindingString(t *testing.T) {
  tests := []string {
    "cpe:2.3:o:intel:ethernet_controller_e810_firmware:*:*:*:*:*:*:*:*",
  }

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

func TestBindingUnmarshalJSON(t *testing.T) {
  passTests := []string {
    `"cpe:2.3:o:intel:ethernet_controller_e810_firmware:*:*:*:*:*:*:*:*"`,
  }

  for _, val := range(passTests) {
    t.Run(val, func(t *testing.T) {
      var b Binding
      if err := json.Unmarshal([]byte(val), &b); err != nil {
        t.Error(err)
      }
    })
  }

  failTests := []struct {
    val string
    exp string
  } {{
    val: `"o:intel:ethernet_controller_e810_firmware:*:*:*:*:*:*:*:*`,
    exp: "unexpected end of JSON input",
  }, {
    val: `"o:intel:ethernet_controller_e810_firmware:*:*:*:*:*:*:*:*"`,
    exp: "missing CPE 2.3 prefix",
  }}

  for _, test := range(failTests) {
    t.Run(test.val, func(t *testing.T) {
      var got Binding
      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)
      }
    })
  }
}

func TestBindingMarshalJSON(t *testing.T) {
  tests := []string {
    "cpe:2.3:o:intel:ethernet_controller_e810_firmware:*:*:*:*:*:*:*:*",
  }

  for _, val := range(tests) {
    t.Run(val, func(t *testing.T) {
      // create binding, check for error
      b, err := NewBinding(val)
      if err != nil {
        t.Error(err)
        return
      }

      // marshal json, check for error
      got, err := json.Marshal(b)
      if err != nil {
        t.Error(err)
        return
      }

      // build/check expected value
      exp := "\"" + val + "\""
      if string(got) != exp {
        t.Errorf("got \"%s\", exp \"%s\"", string(got), exp)
        return
      }
    })
  }
}