aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Duncan <pabs@pablotron.org>2022-02-01 01:21:45 -0500
committerPaul Duncan <pabs@pablotron.org>2022-02-01 01:21:45 -0500
commit07390eeb960df3ca10b758c9f62459cf2ee9aa3f (patch)
tree6612ba4fe2f7252a3096bc2bb250aca11d86636f
parenta824a9732154f9bee2ce530f753d5747de78aa3f (diff)
downloadcvez-07390eeb960df3ca10b758c9f62459cf2ee9aa3f.tar.bz2
cvez-07390eeb960df3ca10b758c9f62459cf2ee9aa3f.zip
add internal/feed/dataformat.go
-rw-r--r--internal/feed/dataformat.go36
-rw-r--r--internal/feed/dataformat_string.go23
-rw-r--r--internal/feed/dataformat_test.go65
-rw-r--r--internal/feed/feed.go27
4 files changed, 125 insertions, 26 deletions
diff --git a/internal/feed/dataformat.go b/internal/feed/dataformat.go
new file mode 100644
index 0000000..bb3f8f8
--- /dev/null
+++ b/internal/feed/dataformat.go
@@ -0,0 +1,36 @@
+package feed
+
+//go:generate stringer -linecomment -type=DataFormat
+
+import (
+ "encoding/json"
+ "fmt"
+)
+
+// Data format for NVD feeds and feed items.
+type DataFormat byte
+
+const (
+ MitreFormat DataFormat = iota // MITRE
+)
+
+// Unmarshal DataFormat from JSON.
+func (me *DataFormat) UnmarshalJSON(b []byte) error {
+ // decode string, check for error
+ var s string
+ if err := json.Unmarshal(b, &s); err != nil {
+ return err
+ }
+
+ // check value
+ switch s {
+ case "MITRE":
+ *me = MitreFormat
+ default:
+ // return error
+ return fmt.Errorf("unknown data format: %s", s)
+ }
+
+ // return success
+ return nil
+}
diff --git a/internal/feed/dataformat_string.go b/internal/feed/dataformat_string.go
new file mode 100644
index 0000000..4b755f4
--- /dev/null
+++ b/internal/feed/dataformat_string.go
@@ -0,0 +1,23 @@
+// Code generated by "stringer -linecomment -type=DataFormat"; DO NOT EDIT.
+
+package feed
+
+import "strconv"
+
+func _() {
+ // An "invalid array index" compiler error signifies that the constant values have changed.
+ // Re-run the stringer command to generate them again.
+ var x [1]struct{}
+ _ = x[MitreFormat-0]
+}
+
+const _DataFormat_name = "MITRE"
+
+var _DataFormat_index = [...]uint8{0, 5}
+
+func (i DataFormat) String() string {
+ if i >= DataFormat(len(_DataFormat_index)-1) {
+ return "DataFormat(" + strconv.FormatInt(int64(i), 10) + ")"
+ }
+ return _DataFormat_name[_DataFormat_index[i]:_DataFormat_index[i+1]]
+}
diff --git a/internal/feed/dataformat_test.go b/internal/feed/dataformat_test.go
new file mode 100644
index 0000000..efb4986
--- /dev/null
+++ b/internal/feed/dataformat_test.go
@@ -0,0 +1,65 @@
+package feed
+
+import (
+ "encoding/json"
+ "testing"
+)
+
+func TestDataFormatUnmarshalInvalidData(t *testing.T) {
+ test := []byte(`{}`)
+ var val DataFormat
+
+ if err := json.Unmarshal(test, &val); err == nil {
+ t.Errorf("got \"%s\", exp error", val)
+ }
+}
+
+func TestDataFormatUnmarshalUnknown(t *testing.T) {
+ test := []byte(`"foo"`)
+ exp := "unknown data format: foo"
+ var val DataFormat
+
+ err := json.Unmarshal(test, &val)
+ if err == nil {
+ t.Errorf("got \"%s\", exp error", val)
+ return
+ }
+
+ if err.Error() != exp {
+ t.Errorf("got \"%s\", exp \"%s\"", err.Error(), exp)
+ }
+}
+
+func TestDataFormatUnmarshalValid(t *testing.T) {
+ test := []byte(`"MITRE"`)
+ exp := MitreFormat
+ var got DataFormat
+
+ if err := json.Unmarshal(test, &got); err != nil {
+ t.Error(err)
+ return
+ }
+
+ if got != exp {
+ t.Errorf("got \"%s\", exp \"%s\"", got, exp)
+ }
+}
+
+func TestDataFormatString(t *testing.T) {
+ tests := []struct {
+ val DataFormat
+ exp string
+ } {
+ { MitreFormat, "MITRE" },
+ { DataFormat(255), "DataFormat(255)" },
+ }
+
+ for _, test := range(tests) {
+ t.Run(test.val.String(), func(t *testing.T) {
+ got := test.val.String()
+ if got != test.exp {
+ t.Errorf("got \"%s\", exp \"%s\"", got, test.exp)
+ }
+ })
+ }
+}
diff --git a/internal/feed/feed.go b/internal/feed/feed.go
index b4d514b..a43b7e9 100644
--- a/internal/feed/feed.go
+++ b/internal/feed/feed.go
@@ -9,8 +9,7 @@ import (
)
const (
- MitreFormat = iota // MITRE data format
- DataVersion40 // Version 4.0
+ DataVersion40 = iota // Version 4.0
OrNodeOp // OR operator
AndNodeOp // And operator
@@ -43,30 +42,6 @@ const (
// TODO: parse cpe
-// Data format for NVD feeds and feed items.
-type DataFormat int
-
-// Unmarshal DataFormat from JSON.
-func (me *DataFormat) UnmarshalJSON(b []byte) error {
- // decode string, check for error
- var s string
- if err := json.Unmarshal(b, &s); err != nil {
- return err
- }
-
- // check value
- switch s {
- case "MITRE":
- *me = MitreFormat
- default:
- // return error
- return fmt.Errorf("unknown data format: %s", s)
- }
-
- // return success
- return nil
-}
-
// Data version for NVD feeds and feed items.
type DataVersion int