package feed import ( "encoding/json" "github.com/pablotron/cvez/cvss" "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) } }) } } func TestVectorMarshalJSON(t *testing.T) { tests := []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(tests) { t.Run(val, func(t *testing.T) { // get expected string exp := "\"" + val + "\"" // create inner vector vec, err := cvss.NewVector(val) if err != nil { t.Error(err) return } // serialize as json buf, err := json.Marshal(Vector { vec }) if err != nil { t.Error(err) return } // check result got := string(buf) if got != exp { t.Errorf("got \"%s\", exp \"%s\"", got, exp) } }) } }