2021-04-23 11:21:27 +03:00
package oval
2019-11-03 21:28:28 +03:00
import (
2019-11-06 03:33:54 +03:00
"errors"
2019-11-03 21:28:28 +03:00
"flag"
"net/http"
"net/http/httptest"
"os"
2021-12-29 15:05:21 +03:00
"path/filepath"
"strings"
2019-11-03 21:28:28 +03:00
"testing"
2023-09-20 09:23:39 +03:00
"github.com/spf13/afero"
2019-11-03 21:28:28 +03:00
"github.com/stretchr/testify/assert"
2020-12-31 16:38:53 +03:00
"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 {
2021-12-29 15:05:21 +03:00
name string
dir string
wantFiles int
wantErr string
2019-11-03 21:28:28 +03:00
} {
{
2021-12-29 15:05:21 +03:00
name : "happy path" ,
dir : "testdata/happy" ,
wantFiles : 24 ,
2019-11-03 21:28:28 +03:00
} ,
{
2021-12-29 15:05:21 +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
} ,
{
2021-12-29 15:05:21 +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
} ,
{
2021-12-29 15:05:21 +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 ) {
2021-12-29 15:05:21 +03:00
ts := httptest . NewServer ( http . FileServer ( http . Dir ( tc . dir ) ) )
2019-11-03 21:28:28 +03:00
defer ts . Close ( )
2021-12-29 15:05:21 +03:00
tmpDir := "/tmp" // It is a virtual filesystem of afero.
2019-11-03 21:28:28 +03:00
appFs := afero . NewMemMapFs ( )
c := Config {
2021-12-29 15:05:21 +03:00
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
}
2021-12-29 15:05:21 +03:00
2019-11-03 21:28:28 +03:00
err := c . Update ( )
2021-12-29 15:05:21 +03:00
if tc . wantErr != "" {
require . Error ( t , err )
assert . Contains ( t , err . Error ( ) , tc . wantErr )
return
2019-11-03 21:28:28 +03:00
}
2021-12-29 15:05:21 +03:00
require . NoError ( t , err , tc . name )
2019-11-03 21:28:28 +03:00
fileCount := 0
2021-12-29 15:05:21 +03:00
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
2021-12-29 15:05:21 +03:00
} else if info . IsDir ( ) {
2019-11-03 21:28:28 +03:00
return nil
}
2020-12-31 16:38:53 +03:00
fileCount ++
2019-11-03 21:28:28 +03:00
2021-12-29 15:05:21 +03:00
got , err := afero . ReadFile ( appFs , path )
2019-11-03 21:28:28 +03:00
assert . NoError ( t , err , tc . name )
2021-12-29 15:05:21 +03:00
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 {
2021-12-29 15:05:21 +03:00
err = os . WriteFile ( goldenPath , got , 0666 )
require . NoError ( t , err , goldenPath )
2019-11-03 21:28:28 +03:00
}
2021-12-29 15:05:21 +03:00
want , err := os . ReadFile ( goldenPath )
2019-11-03 21:28:28 +03:00
assert . NoError ( t , err , tc . name )
2021-12-29 15:05:21 +03:00
assert . JSONEq ( t , string ( want ) , string ( got ) , path )
2019-11-03 21:28:28 +03:00
return nil
} )
assert . NoError ( t , err , tc . name )
2021-12-29 15:05:21 +03:00
assert . Equal ( t , tc . wantFiles , fileCount , tc . name )
2019-11-03 21:28:28 +03:00
} )
}
2019-11-06 03:33:54 +03:00
}
func TestConfig_saveRHSAPerYear ( t * testing . T ) {
testCases := [ ] struct {
2021-12-29 15:05:21 +03:00
name string
rhsaID string
wantErr error
2019-11-06 03:33:54 +03:00
} {
{
2021-12-29 15:05:21 +03:00
name : "happy path" ,
rhsaID : "RHSA-2018:0094" ,
2019-11-06 03:33:54 +03:00
} ,
{
2021-12-29 15:05:21 +03:00
name : "sad path: invalid RHSA-ID format" ,
rhsaID : "foobarbaz" ,
wantErr : errors . New ( "invalid RHSA-ID format" ) ,
2019-11-06 03:33:54 +03:00
} ,
}
for _ , tc := range testCases {
c := Config {
AppFs : afero . NewMemMapFs ( ) ,
}
2019-11-03 21:28:28 +03:00
2021-12-29 15:05:21 +03:00
err := c . saveAdvisoryPerYear ( "/tmp" , tc . rhsaID , Definition { } )
2019-11-06 03:33:54 +03:00
switch {
2021-12-29 15:05:21 +03:00
case tc . wantErr != nil :
assert . Equal ( t , tc . wantErr . Error ( ) , err . Error ( ) , tc . name )
2019-11-06 03:33:54 +03:00
default :
assert . NoError ( t , err , tc . name )
}
}
2019-11-03 21:28:28 +03:00
}