Path: blob/dev/pkg/catalog/loader/loader_test.go
2070 views
package loader12import (3"reflect"4"testing"56"github.com/projectdiscovery/nuclei/v3/pkg/catalog/config"7"github.com/projectdiscovery/nuclei/v3/pkg/catalog/disk"8"github.com/stretchr/testify/require"9)1011func TestLoadTemplates(t *testing.T) {12catalog := disk.NewCatalog("")1314store, err := New(&Config{15Templates: []string{"cves/CVE-2021-21315.yaml"},16Catalog: catalog,17})18require.Nil(t, err, "could not load templates")19require.Equal(t, []string{"cves/CVE-2021-21315.yaml"}, store.finalTemplates, "could not get correct templates")2021templatesDirectory := "/test"22config.DefaultConfig.TemplatesDirectory = templatesDirectory23t.Run("blank", func(t *testing.T) {24store, err := New(&Config{25Catalog: catalog,26})27require.Nil(t, err, "could not load templates")28require.Equal(t, []string{templatesDirectory}, store.finalTemplates, "could not get correct templates")29})30t.Run("only-tags", func(t *testing.T) {31store, err := New(&Config{32Tags: []string{"cves"},33Catalog: catalog,34})35require.Nil(t, err, "could not load templates")36require.Equal(t, []string{templatesDirectory}, store.finalTemplates, "could not get correct templates")37})38t.Run("tags-with-path", func(t *testing.T) {39store, err := New(&Config{40Tags: []string{"cves"},41Catalog: catalog,42})43require.Nil(t, err, "could not load templates")44require.Equal(t, []string{templatesDirectory}, store.finalTemplates, "could not get correct templates")45})46}4748func TestRemoteTemplates(t *testing.T) {49catalog := disk.NewCatalog("")5051var nilStringSlice []string52type args struct {53config *Config54}55tests := []struct {56name string57args args58want *Store59wantErr bool60}{61{62name: "remote-templates-positive",63args: args{64config: &Config{65TemplateURLs: []string{"https://raw.githubusercontent.com/projectdiscovery/nuclei-templates/main/technologies/tech-detect.yaml"},66RemoteTemplateDomainList: []string{"localhost", "raw.githubusercontent.com"},67Catalog: catalog,68},69},70want: &Store{71finalTemplates: []string{"https://raw.githubusercontent.com/projectdiscovery/nuclei-templates/main/technologies/tech-detect.yaml"},72},73wantErr: false,74},75{76name: "remote-templates-negative",77args: args{78config: &Config{79TemplateURLs: []string{"https://raw.githubusercontent.com/projectdiscovery/nuclei-templates/main/technologies/tech-detect.yaml"},80RemoteTemplateDomainList: []string{"localhost"},81Catalog: catalog,82},83},84want: &Store{85finalTemplates: nilStringSlice,86},87wantErr: true,88},89}90for _, tt := range tests {91t.Run(tt.name, func(t *testing.T) {92got, err := New(tt.args.config)93if (err != nil) != tt.wantErr {94t.Errorf("New() error = %v, wantErr %v", err, tt.wantErr)95return96}97if !reflect.DeepEqual(got.finalTemplates, tt.want.finalTemplates) {98t.Errorf("New() = %v, want %v", got.finalTemplates, tt.want.finalTemplates)99}100})101}102}103104105