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 TestSyncConfigGetCisaKevcUrl(t *testing.T) { tests := []struct { name string val string exp string } { { "custom", "https://example.com/", "https://example.com/" }, { "default", "", DefaultConfig.CisaKevcUrl }, } for _, test := range(tests) { t.Run(test.name, func(t *testing.T) { config := SyncConfig { CisaKevcUrl: test.val } got := config.GetCisaKevcUrl() if got != test.exp { t.Errorf("got \"%s\", exp \"%s\"", got, test.exp) } }) } } func TestSyncConfigGetCweListUrl(t *testing.T) { tests := []struct { name string val string exp string } { { "custom", "https://example.com/", "https://example.com/" }, { "default", "", DefaultConfig.CweListUrl }, } for _, test := range(tests) { t.Run(test.name, func(t *testing.T) { config := SyncConfig { CweListUrl: test.val } got := config.GetCweListUrl() 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) } }