From c41d06240249bb4bfb9153b5beb2032df75a7f47 Mon Sep 17 00:00:00 2001 From: Paul Duncan Date: Wed, 16 Mar 2022 08:24:06 -0400 Subject: dbstore/dbstore_test.go: add TestAddCisaCatalogs() and stub TestCisaSearch() --- dbstore/dbstore_test.go | 85 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) (limited to 'dbstore/dbstore_test.go') diff --git a/dbstore/dbstore_test.go b/dbstore/dbstore_test.go index df7dd62..524fecb 100644 --- a/dbstore/dbstore_test.go +++ b/dbstore/dbstore_test.go @@ -10,6 +10,7 @@ import ( "errors" "fmt" _ "github.com/mattn/go-sqlite3" + "github.com/pablotron/cvez/cisa" "github.com/pablotron/cvez/cpedict" "github.com/pablotron/cvez/cpematch" nvd_feed "github.com/pablotron/cvez/feed" @@ -42,6 +43,27 @@ func getFeed(path string) (nvd_feed.Feed, error) { return feed, d.Decode(&feed) } +// Load gzip-compressed test CISA catalog. +func getCisaCatalog(path string) (cisa.Catalog, error) { + var cat cisa.Catalog + + // open file for reading + file, err := os.Open(path) + if err != nil { + return cat, err + } + + // wrap in reader, return success + src, err := gzip.NewReader(file) + if err != nil { + return cat, err + } + + // create decoder, decode catalog, return result + d := json.NewDecoder(src) + return cat, d.Decode(&cat) +} + func getTestDictionary(path string) (cpedict.Dictionary, error) { var dict cpedict.Dictionary @@ -1376,3 +1398,66 @@ func TestCveSearch(t *testing.T) { } }) } + +func TestAddCisaCatalogs(t *testing.T) { + ctx := context.Background() + + tests := []struct { + name string // test name + files []string // test files + fast bool // is this test fast? + } {{ + name: "fast", + files: []string { "cisa-kevc-20220313-tiny", }, + fast: true, + }, { + name: "slow", + files: []string { "cisa-kevc-20220313-tiny", "cisa-kevc-20220313" }, + fast: false, + }} + + for _, test := range(tests) { + t.Run(test.name, func(t *testing.T) { + // skip slow tests in short mode + if !test.fast && testing.Short() { + t.Skip("skipping in short mode") + return + } + + // build db path + dbPath := fmt.Sprintf("testdata/test-addcisacatalogs-%s.db", test.name) + + // create test db + db, err := createTestDb(ctx, dbPath) + if err != nil { + t.Error(err) + return + } + + // load test catalogs + cats := make([]cisa.Catalog, len(test.files)) + for i, name := range(test.files) { + // build catalog path + path := fmt.Sprintf("testdata/%s.json.gz", name) + + // get catalog + if cat, err := getCisaCatalog(path); err != nil { + t.Error(err) + return + } else { + cats[i] = cat + } + } + + // add catalogs + if _, err = db.AddCisaCatalogs(ctx, cats); err != nil { + t.Error(err) + return + } + }) + } +} + +// TODO: TestCisaSearch() +// test db: dbstore/testdata/mock-testcisasearch.db +// test cases: "wordpad" (1 result), "microsoft excel" (4 results) -- cgit v1.2.3