2021-06-30 18:39:01 +05:30
|
|
|
package loader
|
|
|
|
|
|
|
|
|
|
import (
|
2022-01-21 20:14:50 +05:30
|
|
|
"reflect"
|
2021-06-30 18:39:01 +05:30
|
|
|
"testing"
|
|
|
|
|
|
2023-10-17 17:44:13 +05:30
|
|
|
"github.com/projectdiscovery/nuclei/v3/pkg/catalog/config"
|
|
|
|
|
"github.com/projectdiscovery/nuclei/v3/pkg/catalog/disk"
|
2021-06-30 18:39:01 +05:30
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestLoadTemplates(t *testing.T) {
|
2022-08-10 23:35:58 +05:30
|
|
|
catalog := disk.NewCatalog("")
|
|
|
|
|
|
2021-06-30 18:39:01 +05:30
|
|
|
store, err := New(&Config{
|
|
|
|
|
Templates: []string{"cves/CVE-2021-21315.yaml"},
|
2022-08-10 23:35:58 +05:30
|
|
|
Catalog: catalog,
|
2021-06-30 18:39:01 +05:30
|
|
|
})
|
|
|
|
|
require.Nil(t, err, "could not load templates")
|
|
|
|
|
require.Equal(t, []string{"cves/CVE-2021-21315.yaml"}, store.finalTemplates, "could not get correct templates")
|
|
|
|
|
|
2021-07-02 15:46:42 +05:30
|
|
|
templatesDirectory := "/test"
|
2023-04-19 21:58:48 +05:30
|
|
|
config.DefaultConfig.TemplatesDirectory = templatesDirectory
|
2021-06-30 18:39:01 +05:30
|
|
|
t.Run("blank", func(t *testing.T) {
|
|
|
|
|
store, err := New(&Config{
|
2023-04-19 21:58:48 +05:30
|
|
|
Catalog: catalog,
|
2021-06-30 18:39:01 +05:30
|
|
|
})
|
|
|
|
|
require.Nil(t, err, "could not load templates")
|
2021-07-02 15:46:42 +05:30
|
|
|
require.Equal(t, []string{templatesDirectory}, store.finalTemplates, "could not get correct templates")
|
2021-06-30 18:39:01 +05:30
|
|
|
})
|
|
|
|
|
t.Run("only-tags", func(t *testing.T) {
|
|
|
|
|
store, err := New(&Config{
|
2023-04-19 21:58:48 +05:30
|
|
|
Tags: []string{"cves"},
|
|
|
|
|
Catalog: catalog,
|
2021-06-30 18:39:01 +05:30
|
|
|
})
|
|
|
|
|
require.Nil(t, err, "could not load templates")
|
2021-07-02 15:46:42 +05:30
|
|
|
require.Equal(t, []string{templatesDirectory}, store.finalTemplates, "could not get correct templates")
|
2021-06-30 18:39:01 +05:30
|
|
|
})
|
|
|
|
|
t.Run("tags-with-path", func(t *testing.T) {
|
|
|
|
|
store, err := New(&Config{
|
2023-04-19 21:58:48 +05:30
|
|
|
Tags: []string{"cves"},
|
|
|
|
|
Catalog: catalog,
|
2021-06-30 18:39:01 +05:30
|
|
|
})
|
|
|
|
|
require.Nil(t, err, "could not load templates")
|
2021-07-02 15:46:42 +05:30
|
|
|
require.Equal(t, []string{templatesDirectory}, store.finalTemplates, "could not get correct templates")
|
2021-06-30 18:39:01 +05:30
|
|
|
})
|
|
|
|
|
}
|
2022-01-21 20:14:50 +05:30
|
|
|
|
|
|
|
|
func TestRemoteTemplates(t *testing.T) {
|
2022-08-10 23:35:58 +05:30
|
|
|
catalog := disk.NewCatalog("")
|
|
|
|
|
|
2022-01-21 20:14:50 +05:30
|
|
|
var nilStringSlice []string
|
|
|
|
|
type args struct {
|
|
|
|
|
config *Config
|
|
|
|
|
}
|
|
|
|
|
tests := []struct {
|
|
|
|
|
name string
|
|
|
|
|
args args
|
|
|
|
|
want *Store
|
|
|
|
|
wantErr bool
|
|
|
|
|
}{
|
|
|
|
|
{
|
|
|
|
|
name: "remote-templates-positive",
|
|
|
|
|
args: args{
|
|
|
|
|
config: &Config{
|
2023-03-31 12:57:15 +02:00
|
|
|
TemplateURLs: []string{"https://raw.githubusercontent.com/projectdiscovery/nuclei-templates/main/technologies/tech-detect.yaml"},
|
2022-08-10 23:35:58 +05:30
|
|
|
RemoteTemplateDomainList: []string{"localhost", "raw.githubusercontent.com"},
|
|
|
|
|
Catalog: catalog,
|
2022-01-21 20:14:50 +05:30
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
want: &Store{
|
2023-03-31 12:57:15 +02:00
|
|
|
finalTemplates: []string{"https://raw.githubusercontent.com/projectdiscovery/nuclei-templates/main/technologies/tech-detect.yaml"},
|
2022-01-21 20:14:50 +05:30
|
|
|
},
|
|
|
|
|
wantErr: false,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "remote-templates-negative",
|
|
|
|
|
args: args{
|
|
|
|
|
config: &Config{
|
2023-03-31 12:57:15 +02:00
|
|
|
TemplateURLs: []string{"https://raw.githubusercontent.com/projectdiscovery/nuclei-templates/main/technologies/tech-detect.yaml"},
|
2022-01-21 20:14:50 +05:30
|
|
|
RemoteTemplateDomainList: []string{"localhost"},
|
2022-08-10 23:35:58 +05:30
|
|
|
Catalog: catalog,
|
2022-01-21 20:14:50 +05:30
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
want: &Store{
|
|
|
|
|
finalTemplates: nilStringSlice,
|
|
|
|
|
},
|
|
|
|
|
wantErr: true,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
|
got, err := New(tt.args.config)
|
|
|
|
|
if (err != nil) != tt.wantErr {
|
|
|
|
|
t.Errorf("New() error = %v, wantErr %v", err, tt.wantErr)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if !reflect.DeepEqual(got.finalTemplates, tt.want.finalTemplates) {
|
|
|
|
|
t.Errorf("New() = %v, want %v", got.finalTemplates, tt.want.finalTemplates)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|