vuln-list-update/redhat/oval/redhat_test.go

138 lines
3.0 KiB
Go
Raw Normal View History

package oval
2019-11-03 21:28:28 +03:00
import (
"errors"
2019-11-03 21:28:28 +03:00
"flag"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
2019-11-03 21:28:28 +03:00
"testing"
"github.com/spf13/afero"
2019-11-03 21:28:28 +03:00
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
2019-11-03 21:28:28 +03:00
)
var update = flag.Bool("update", false, "update golden files")
func TestConfig_Update(t *testing.T) {
testCases := []struct {
name string
dir string
wantFiles int
wantErr string
2019-11-03 21:28:28 +03:00
}{
{
name: "happy path",
dir: "testdata/happy",
wantFiles: 24,
2019-11-03 21:28:28 +03:00
},
{
name: "404",
dir: "testdata/missing-oval",
wantErr: "failed to fetch Red Hat OVAL v2: failed to fetch URL: HTTP error. status code: 404, url:",
2019-11-03 21:28:28 +03:00
},
{
name: "invalid file format",
dir: "testdata/invalid-bzip2",
wantErr: "failed to unmarshal Red Hat OVAL v2 XML: bzip2 data invalid: bad magic value",
2019-11-03 21:28:28 +03:00
},
{
name: "broken XML",
dir: "testdata/broken-xml",
wantErr: "failed to unmarshal Red Hat OVAL v2 XML: XML syntax error on line 411: element",
2019-11-03 21:28:28 +03:00
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
ts := httptest.NewServer(http.FileServer(http.Dir(tc.dir)))
2019-11-03 21:28:28 +03:00
defer ts.Close()
tmpDir := "/tmp" // It is a virtual filesystem of afero.
2019-11-03 21:28:28 +03:00
appFs := afero.NewMemMapFs()
c := Config{
VulnListDir: tmpDir,
URLFormat: ts.URL + "/%s",
RepoToCpeURL: ts.URL + "/repository-to-cpe.json",
AppFs: appFs,
Retry: 0,
2019-11-03 21:28:28 +03:00
}
2019-11-03 21:28:28 +03:00
err := c.Update()
if tc.wantErr != "" {
require.Error(t, err)
assert.Contains(t, err.Error(), tc.wantErr)
return
2019-11-03 21:28:28 +03:00
}
require.NoError(t, err, tc.name)
2019-11-03 21:28:28 +03:00
fileCount := 0
root := tmpDir + "/oval"
err = afero.Walk(appFs, root, func(path string, info os.FileInfo, err error) error {
2019-11-03 21:28:28 +03:00
if err != nil {
return err
} else if info.IsDir() {
2019-11-03 21:28:28 +03:00
return nil
}
fileCount++
2019-11-03 21:28:28 +03:00
got, err := afero.ReadFile(appFs, path)
2019-11-03 21:28:28 +03:00
assert.NoError(t, err, tc.name)
rel, err := filepath.Rel(root, path)
require.NoError(t, err)
rel = strings.ReplaceAll(rel, ":", "-")
goldenPath := filepath.Join("testdata", "golden", rel)
2019-11-03 21:28:28 +03:00
if *update {
err = os.WriteFile(goldenPath, got, 0666)
require.NoError(t, err, goldenPath)
2019-11-03 21:28:28 +03:00
}
want, err := os.ReadFile(goldenPath)
2019-11-03 21:28:28 +03:00
assert.NoError(t, err, tc.name)
assert.JSONEq(t, string(want), string(got), path)
2019-11-03 21:28:28 +03:00
return nil
})
assert.NoError(t, err, tc.name)
assert.Equal(t, tc.wantFiles, fileCount, tc.name)
2019-11-03 21:28:28 +03:00
})
}
}
func TestConfig_saveRHSAPerYear(t *testing.T) {
testCases := []struct {
name string
rhsaID string
wantErr error
}{
{
name: "happy path",
rhsaID: "RHSA-2018:0094",
},
{
name: "sad path: invalid RHSA-ID format",
rhsaID: "foobarbaz",
wantErr: errors.New("invalid RHSA-ID format"),
},
}
for _, tc := range testCases {
c := Config{
AppFs: afero.NewMemMapFs(),
}
2019-11-03 21:28:28 +03:00
err := c.saveAdvisoryPerYear("/tmp", tc.rhsaID, Definition{})
switch {
case tc.wantErr != nil:
assert.Equal(t, tc.wantErr.Error(), err.Error(), tc.name)
default:
assert.NoError(t, err, tc.name)
}
}
2019-11-03 21:28:28 +03:00
}