diff options
author | Paul Duncan <pabs@pablotron.org> | 2022-02-24 01:38:28 -0500 |
---|---|---|
committer | Paul Duncan <pabs@pablotron.org> | 2022-02-24 01:38:28 -0500 |
commit | d0f32435f83c78c2d00c672400ba04cbed5da97a (patch) | |
tree | 08f66421dfbc411fc6482b24e2d9dbef479fc7c7 /nvdmirror | |
parent | 2615a8f217796caa3a0572c25574f63055247edf (diff) | |
download | cvez-d0f32435f83c78c2d00c672400ba04cbed5da97a.tar.bz2 cvez-d0f32435f83c78c2d00c672400ba04cbed5da97a.zip |
add nvdmirror/syncconfig_test.go
Diffstat (limited to 'nvdmirror')
-rw-r--r-- | nvdmirror/syncconfig_test.go | 158 |
1 files changed, 158 insertions, 0 deletions
diff --git a/nvdmirror/syncconfig_test.go b/nvdmirror/syncconfig_test.go new file mode 100644 index 0000000..cc1cdfe --- /dev/null +++ b/nvdmirror/syncconfig_test.go @@ -0,0 +1,158 @@ +package nvdmirror + +import ( + "fmt" + "reflect" + "testing" + "time" +) + +// base url for tests +const testBaseUrl = "https://example.com" + +func TestSyncConfigUserAgent(t *testing.T) { + tests := []struct { + name string + val string + exp string + } { + { "random", "foo-bar", "foo-bar" }, + { "default", "", DefaultConfig.UserAgent }, + } + + for _, test := range(tests) { + t.Run(test.name, func(t *testing.T) { + config := SyncConfig { UserAgent: test.val } + + got := config.GetUserAgent() + if got != test.exp { + t.Errorf("got \"%s\", exp \"%s\"", got, test.exp) + } + }) + } +} + +func TestSyncConfigGetCveUrl(t *testing.T) { + tests := []struct { + id string + ext string + exp string + } { + { "foo", "meta", "https://example.com/nvdcve-1.1-foo.meta" }, + { "bar", "json.gz", "https://example.com/nvdcve-1.1-bar.json.gz" }, + } + + for _, test := range(tests) { + t.Run(test.id, func(t *testing.T) { + config := SyncConfig { Cve11BaseUrl: testBaseUrl } + + got := config.GetCveUrl(test.id, test.ext) + if got != test.exp { + t.Errorf("got \"%s\", exp \"%s\"", got, test.exp) + } + }) + } +} + +func TestSyncConfigGetCveYearUrl(t *testing.T) { + tests := []struct { + year int + exp string + } { + { 2022, "https://example.com/nvdcve-1.1-2022.meta" }, + { 4, "https://example.com/nvdcve-1.1-0004.meta" }, + } + + for _, test := range(tests) { + t.Run(fmt.Sprintf("%04d", test.year), func(t *testing.T) { + config := SyncConfig { Cve11BaseUrl: testBaseUrl } + + got := config.GetCveYearUrl(test.year, "meta") + if got != test.exp { + t.Errorf("got \"%s\", exp \"%s\"", got, test.exp) + } + }) + } +} + +func TestSyncConfigGetCpeMatchUrl(t *testing.T) { + tests := []struct { + ext string + exp string + } { + { "meta", "https://example.com/nvdcpematch-1.0.meta" }, + { "json.gz", "https://example.com/nvdcpematch-1.0.json.gz" }, + { "whatever", "https://example.com/nvdcpematch-1.0.whatever" }, + } + + for _, test := range(tests) { + t.Run(test.ext, func(t *testing.T) { + config := SyncConfig { CpeMatch10BaseUrl: testBaseUrl } + + got := config.GetCpeMatchUrl(test.ext) + if got != test.exp { + t.Errorf("got \"%s\", exp \"%s\"", got, test.exp) + } + }) + } +} + +func TestSyncConfigGetCpeDictUrl(t *testing.T) { + tests := []struct { + name string + val string + exp string + } { + { "custom", "https://example.com/", "https://example.com/" }, + { "default", "", DefaultConfig.Cpe23DictUrl }, + } + + for _, test := range(tests) { + t.Run(test.name, func(t *testing.T) { + config := SyncConfig { Cpe23DictUrl: test.val } + + got := config.GetCpeDictUrl() + if got != test.exp { + t.Errorf("got \"%s\", exp \"%s\"", got, test.exp) + } + }) + } +} + +func TestSyncConfigGetMetaUrls(t *testing.T) { + // declare expected result + exp := make(map[string]string) + + // add years + for year := 2002; year <= time.Now().Year(); year++ { + k := fmt.Sprintf("%s/nvdcve-1.1-%04d.meta", testBaseUrl, year) + v := fmt.Sprintf("%s/nvdcve-1.1-%04d.json.gz", testBaseUrl, year) + exp[k] = v + } + + // add cve extra feeds + for _, id := range([]string { "modified", "recent" }) { + k := fmt.Sprintf("%s/nvdcve-1.1-%s.meta", testBaseUrl, id) + v := fmt.Sprintf("%s/nvdcve-1.1-%s.json.gz", testBaseUrl, id) + exp[k] = v + } + + { + // add cpe match + k := fmt.Sprintf("%s/nvdcpematch-1.0.meta", testBaseUrl) + v := fmt.Sprintf("%s/nvdcpematch-1.0.json.gz", testBaseUrl) + exp[k] = v + } + + // declare custom config + config := SyncConfig { + Cve11BaseUrl: testBaseUrl, + CpeMatch10BaseUrl: testBaseUrl, + } + + // get/check meta urls + got := config.getMetaUrls() + if !reflect.DeepEqual(got, exp) { + t.Errorf("got \"%v\", exp \"%v\"", got, exp) + } +} |