aboutsummaryrefslogtreecommitdiff
path: root/internal/cpe/binding_test.go
diff options
context:
space:
mode:
authorPaul Duncan <pabs@pablotron.org>2022-02-03 01:00:45 -0500
committerPaul Duncan <pabs@pablotron.org>2022-02-03 01:00:45 -0500
commitbc02cdde7024c528cb3ccb05b88329850a3ba512 (patch)
treececa6123c6990585607313ef0bf0e84d51150ef4 /internal/cpe/binding_test.go
parent5db2e66f30d671f334486ad3dfbb178f4fef6817 (diff)
downloadcvez-bc02cdde7024c528cb3ccb05b88329850a3ba512.tar.bz2
cvez-bc02cdde7024c528cb3ccb05b88329850a3ba512.zip
internal/cpe: add binding and tests
Diffstat (limited to 'internal/cpe/binding_test.go')
-rw-r--r--internal/cpe/binding_test.go132
1 files changed, 132 insertions, 0 deletions
diff --git a/internal/cpe/binding_test.go b/internal/cpe/binding_test.go
new file mode 100644
index 0000000..7c5154a
--- /dev/null
+++ b/internal/cpe/binding_test.go
@@ -0,0 +1,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
+ }
+ })
+ }
+}