2022-10-04 11:04:48 +00:00
package nvd
2022-06-01 16:06:57 +00:00
import (
"context"
"path/filepath"
"strings"
"testing"
2023-04-21 23:37:29 +00:00
"github.com/fleetdm/fleet/v4/server/contexts/license"
2022-06-01 16:06:57 +00:00
"github.com/fleetdm/fleet/v4/pkg/nettest"
"github.com/fleetdm/fleet/v4/server/fleet"
"github.com/fleetdm/fleet/v4/server/mock"
2024-06-17 13:27:31 +00:00
"github.com/go-kit/log"
2022-06-01 16:06:57 +00:00
"github.com/stretchr/testify/require"
"github.com/tj/assert"
)
func TestDownloadEPSSFeed ( t * testing . T ) {
nettest . Run ( t )
tempDir := t . TempDir ( )
2023-01-03 17:56:11 +00:00
err := DownloadEPSSFeed ( tempDir )
2022-06-01 16:06:57 +00:00
require . NoError ( t , err )
assert . FileExists ( t , filepath . Join ( tempDir , strings . TrimSuffix ( epssFilename , ".gz" ) ) )
}
func TestDownloadCISAKnownExploitsFeed ( t * testing . T ) {
nettest . Run ( t )
tempDir := t . TempDir ( )
2025-08-13 18:35:45 +00:00
err := DownloadCISAKnownExploitsFeed ( tempDir , "" )
require . NoError ( t , err )
assert . FileExists ( t , filepath . Join ( tempDir , cisaKnownExploitsFilename ) )
}
func TestDownloadCISAKnownExploitsFeedMirror ( t * testing . T ) {
nettest . Run ( t )
tempDir := t . TempDir ( )
err := DownloadCISAKnownExploitsFeed ( tempDir , "https://raw.githubusercontent.com/EugenMayer/cisa-known-exploited-mirror/main/known_exploited_vulnerabilities.json" )
2022-06-01 16:06:57 +00:00
require . NoError ( t , err )
assert . FileExists ( t , filepath . Join ( tempDir , cisaKnownExploitsFilename ) )
}
func TestLoadCVEMeta ( t * testing . T ) {
ds := new ( mock . Store )
2022-06-03 17:37:47 +00:00
var cveMeta [ ] fleet . CVEMeta
ds . InsertCVEMetaFunc = func ( ctx context . Context , x [ ] fleet . CVEMeta ) error {
cveMeta = x
2022-06-01 16:06:57 +00:00
return nil
}
2022-06-08 19:15:44 +00:00
logger := log . NewNopLogger ( )
2023-02-17 15:00:57 +00:00
err := LoadCVEMeta ( license . NewContext ( context . Background ( ) , & fleet . LicenseInfo {
Tier : "premium" ,
} ) , logger , "../testdata" , ds )
2022-06-01 16:06:57 +00:00
require . NoError ( t , err )
require . True ( t , ds . InsertCVEMetaFuncInvoked )
2022-06-03 17:37:47 +00:00
// check some cves to make sure they got loaded correctly
metaMap := make ( map [ string ] fleet . CVEMeta )
for _ , meta := range cveMeta {
metaMap [ meta . CVE ] = meta
}
meta := metaMap [ "CVE-2022-29676" ]
require . Equal ( t , float64 ( 7.2 ) , * meta . CVSSScore )
require . Equal ( t , float64 ( 0.00885 ) , * meta . EPSSProbability )
require . Equal ( t , false , * meta . CISAKnownExploit )
2023-09-15 17:24:10 +00:00
require . Equal (
t ,
"CSCMS Music Portal System v4.2 was discovered to contain a SQL injection vulnerability via the id parameter at /admin.php/pic/admin/lists/zhuan." ,
2024-11-05 17:16:24 +00:00
meta . Description ,
2023-09-15 17:24:10 +00:00
)
2022-06-03 17:37:47 +00:00
meta = metaMap [ "CVE-2022-22587" ]
2023-04-21 23:37:29 +00:00
require . Equal ( t , float64 ( 9.8 ) , * meta . CVSSScore )
2022-06-03 17:37:47 +00:00
require . Equal ( t , float64 ( 0.01843 ) , * meta . EPSSProbability )
require . Equal ( t , true , * meta . CISAKnownExploit )
2023-09-15 17:24:10 +00:00
require . Equal (
t ,
"A memory corruption issue was addressed with improved input validation. This issue is fixed in iOS 15.3 and iPadOS 15.3, macOS Big Sur 11.6.3, macOS Monterey 12.2. A malicious application may be able to execute arbitrary code with kernel privileges. Apple is aware of a report that this issue may have been actively exploited.." ,
2024-11-05 17:16:24 +00:00
meta . Description ,
2023-09-15 17:24:10 +00:00
)
2022-06-01 16:06:57 +00:00
}
2022-09-01 16:02:07 +00:00
func TestDownloadCPETranslations ( t * testing . T ) {
nettest . Run ( t )
tempDir := t . TempDir ( )
2023-01-03 17:56:11 +00:00
err := DownloadCPETranslationsFromGithub ( tempDir , "" )
2022-09-01 16:02:07 +00:00
require . NoError ( t , err )
assert . FileExists ( t , filepath . Join ( tempDir , cpeTranslationsFilename ) )
}