fleet/server/vulnerabilities/cve_test.go
Tomas Touceda f8b7a83cc6
Process stored CPEs and store found CVEs (#1533)
* WIP

* WIP

* Make path optional and fix tests

* Add first generate

* Move to nvd package

* remove replace

* Re-add replace

* It's path, not file name

* Change how db path is set and use etag

* Fix typos

* Make db generation faster

* Remove quotes

* Doesn't like comments

* Samitize etag and save to file

* Refactor some things and improve writing of etagenv

* Compress file and truncate amount of items for faster testing

* Remove quotes

* Try to improve performance

* Ignore truncate error if not exists

* Minor cleanup and make sqlite have cpe prefix

* Simplify code and test sync

* Add VCR for sync test

* Check for nvdRelease nil

* Add test for the actual translation

* Address review comments

* Rename generate command because we'll have a cve one too

* Move to its own dir

* Add first cve db generation

* WIP but with final strategy, preparring to merge main

* Fix merge conflicts

* WIP

* wip

* Insert CVEs to the db

* Remove unused code

* Use wg instead of counting

* Call cancelFunc to avoid ctx leak

* Fix logs for better readability

* Point code to fleetdm instead of my repo
2021-08-04 18:01:39 -03:00

54 lines
1.3 KiB
Go

package vulnerabilities
import (
"context"
"os"
"sync"
"testing"
"github.com/fleetdm/fleet/v4/server/mock"
kitlog "github.com/go-kit/kit/log"
"github.com/stretchr/testify/require"
)
var cvetests = []struct {
cpe, cve string
}{
{"cpe:2.3:a:1password:1password:3.9.9:*:*:*:*:macos:*:*", "CVE-2012-6369"},
{"cpe:2.3:a:1password:1password:3.9.9:*:*:*:*:*:*:*", "CVE-2012-6369"},
}
func TestTranslateCPEToCVE(t *testing.T) {
tempDir, err := os.MkdirTemp(os.TempDir(), "TestTranslateCPEToCVE-*")
require.NoError(t, err)
defer os.RemoveAll(tempDir)
ds := new(mock.Store)
ctx := context.Background()
for _, tt := range cvetests {
t.Run(tt.cpe, func(t *testing.T) {
ds.AllCPEsFunc = func() ([]string, error) {
return []string{tt.cpe}, nil
}
cveLock := &sync.Mutex{}
cveToCPEs := make(map[string][]string)
var cvesFound []string
ds.InsertCVEForCPEFunc = func(cve string, cpes []string) error {
cveLock.Lock()
defer cveLock.Unlock()
cveToCPEs[cve] = cpes
cvesFound = append(cvesFound, cve)
return nil
}
err = TranslateCPEToCVE(ctx, ds, tempDir, kitlog.NewLogfmtLogger(os.Stdout))
require.NoError(t, err)
require.Equal(t, []string{tt.cve}, cvesFound)
require.Equal(t, []string{tt.cpe}, cveToCPEs[tt.cve])
})
}
}