2021-10-20 01:31:38 +03:00
package parsers
import (
"errors"
2021-10-20 23:14:04 +03:00
"fmt"
2021-10-20 01:31:38 +03:00
"testing"
2022-08-10 23:35:58 +05:30
"github.com/projectdiscovery/nuclei/v2/pkg/catalog/disk"
2021-10-20 01:31:38 +03:00
"github.com/projectdiscovery/nuclei/v2/pkg/catalog/loader/filter"
"github.com/projectdiscovery/nuclei/v2/pkg/model"
2023-04-27 12:57:30 +03:00
"github.com/projectdiscovery/nuclei/v2/pkg/model/types/severity"
2021-10-20 01:31:38 +03:00
"github.com/projectdiscovery/nuclei/v2/pkg/model/types/stringslice"
"github.com/projectdiscovery/nuclei/v2/pkg/templates"
2023-02-26 03:47:47 +08:00
"github.com/stretchr/testify/require"
2021-10-20 01:31:38 +03:00
)
func TestLoadTemplate ( t * testing . T ) {
2022-08-10 23:35:58 +05:30
catalog := disk . NewCatalog ( "" )
2021-10-20 01:31:38 +03:00
origTemplatesCache := parsedTemplatesCache
defer func ( ) { parsedTemplatesCache = origTemplatesCache } ( )
tt := [ ] struct {
name string
template * templates . Template
templateErr error
expectedErr error
2023-04-27 12:57:30 +03:00
isValid bool
2021-10-20 01:31:38 +03:00
} {
{
name : "valid" ,
template : & templates . Template {
ID : "CVE-2021-27330" ,
Info : model . Info {
2023-04-27 12:57:30 +03:00
Name : "Valid template" ,
Authors : stringslice . StringSlice { Value : "Author" } ,
SeverityHolder : severity . Holder { Severity : severity . Medium } ,
2021-10-20 01:31:38 +03:00
} ,
} ,
2023-04-27 12:57:30 +03:00
isValid : true ,
2021-10-20 01:31:38 +03:00
} ,
{
2021-10-20 23:24:11 +03:00
name : "emptyTemplate" ,
2021-10-20 01:31:38 +03:00
template : & templates . Template { } ,
2023-04-27 12:57:30 +03:00
isValid : false ,
expectedErr : errors . New ( "mandatory 'name' field is missing, mandatory 'author' field is missing, mandatory 'id' field is missing, with syntax warning: field 'severity' is missing" ) ,
2021-10-20 23:24:11 +03:00
} ,
{
name : "emptyNameWithInvalidID" ,
template : & templates . Template {
ID : "invalid id" ,
Info : model . Info {
2023-04-27 12:57:30 +03:00
Authors : stringslice . StringSlice { Value : "Author" } ,
SeverityHolder : severity . Holder { Severity : severity . Medium } ,
2021-10-20 23:24:11 +03:00
} ,
} ,
expectedErr : errors . New ( "mandatory 'name' field is missing, invalid field format for 'id' (allowed format is ^([a-zA-Z0-9]+[-_])*[a-zA-Z0-9]+$)" ) ,
2021-10-20 01:31:38 +03:00
} ,
2023-04-27 12:57:30 +03:00
{
name : "emptySeverity" ,
template : & templates . Template {
ID : "CVE-2021-27330" ,
Info : model . Info {
Name : "Valid template" ,
Authors : stringslice . StringSlice { Value : "Author" } ,
} ,
} ,
isValid : true ,
expectedErr : errors . New ( "field 'severity' is missing" ) ,
} ,
2021-10-20 01:31:38 +03:00
}
for _ , tc := range tt {
t . Run ( tc . name , func ( t * testing . T ) {
parsedTemplatesCache . Store ( tc . name , tc . template , tc . templateErr )
2022-08-25 13:22:08 +02:00
tagFilter , err := filter . New ( & filter . Config { } )
require . Nil ( t , err )
2022-08-10 23:35:58 +05:30
success , err := LoadTemplate ( tc . name , tagFilter , nil , catalog )
2023-04-27 12:57:30 +03:00
require . Equal ( t , tc . isValid , success )
2021-10-20 01:31:38 +03:00
if tc . expectedErr == nil {
require . NoError ( t , err )
} else {
2023-04-27 12:57:30 +03:00
require . ErrorContains ( t , err , tc . expectedErr . Error ( ) )
2021-10-20 01:31:38 +03:00
}
} )
}
2021-10-20 23:14:04 +03:00
t . Run ( "invalidTemplateID" , func ( t * testing . T ) {
tt := [ ] struct {
id string
success bool
} {
{ id : "A-B-C" , success : true } ,
{ id : "A-B-C-1" , success : true } ,
{ id : "CVE_2021_27330" , success : true } ,
{ id : "ABC DEF" , success : false } ,
{ id : "_-__AAA_" , success : false } ,
{ id : " CVE-2021-27330" , success : false } ,
{ id : "CVE-2021-27330 " , success : false } ,
{ id : "CVE-2021-27330-" , success : false } ,
{ id : "-CVE-2021-27330-" , success : false } ,
{ id : "CVE-2021--27330" , success : false } ,
{ id : "CVE-2021+27330" , success : false } ,
}
for i , tc := range tt {
name := fmt . Sprintf ( "regexp%d" , i )
t . Run ( name , func ( t * testing . T ) {
template := & templates . Template {
ID : tc . id ,
Info : model . Info {
2023-04-27 12:57:30 +03:00
Name : "Valid template" ,
Authors : stringslice . StringSlice { Value : "Author" } ,
SeverityHolder : severity . Holder { Severity : severity . Medium } ,
2021-10-20 23:14:04 +03:00
} ,
}
parsedTemplatesCache . Store ( name , template , nil )
2022-08-25 13:22:08 +02:00
tagFilter , err := filter . New ( & filter . Config { } )
require . Nil ( t , err )
2022-08-10 23:35:58 +05:30
success , err := LoadTemplate ( name , tagFilter , nil , catalog )
2021-10-20 23:14:04 +03:00
if tc . success {
require . NoError ( t , err )
require . True ( t , success )
} else {
2023-04-27 12:57:30 +03:00
require . ErrorContains ( t , err , "invalid field format for 'id' (allowed format is ^([a-zA-Z0-9]+[-_])*[a-zA-Z0-9]+$)" )
2021-10-20 23:14:04 +03:00
require . False ( t , success )
}
} )
}
} )
2021-10-20 01:31:38 +03:00
}