Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
projectdiscovery
GitHub Repository: projectdiscovery/nuclei
Path: blob/dev/pkg/catalog/loader/loader_test.go
2070 views
1
package loader
2
3
import (
4
"reflect"
5
"testing"
6
7
"github.com/projectdiscovery/nuclei/v3/pkg/catalog/config"
8
"github.com/projectdiscovery/nuclei/v3/pkg/catalog/disk"
9
"github.com/stretchr/testify/require"
10
)
11
12
func TestLoadTemplates(t *testing.T) {
13
catalog := disk.NewCatalog("")
14
15
store, err := New(&Config{
16
Templates: []string{"cves/CVE-2021-21315.yaml"},
17
Catalog: catalog,
18
})
19
require.Nil(t, err, "could not load templates")
20
require.Equal(t, []string{"cves/CVE-2021-21315.yaml"}, store.finalTemplates, "could not get correct templates")
21
22
templatesDirectory := "/test"
23
config.DefaultConfig.TemplatesDirectory = templatesDirectory
24
t.Run("blank", func(t *testing.T) {
25
store, err := New(&Config{
26
Catalog: catalog,
27
})
28
require.Nil(t, err, "could not load templates")
29
require.Equal(t, []string{templatesDirectory}, store.finalTemplates, "could not get correct templates")
30
})
31
t.Run("only-tags", func(t *testing.T) {
32
store, err := New(&Config{
33
Tags: []string{"cves"},
34
Catalog: catalog,
35
})
36
require.Nil(t, err, "could not load templates")
37
require.Equal(t, []string{templatesDirectory}, store.finalTemplates, "could not get correct templates")
38
})
39
t.Run("tags-with-path", func(t *testing.T) {
40
store, err := New(&Config{
41
Tags: []string{"cves"},
42
Catalog: catalog,
43
})
44
require.Nil(t, err, "could not load templates")
45
require.Equal(t, []string{templatesDirectory}, store.finalTemplates, "could not get correct templates")
46
})
47
}
48
49
func TestRemoteTemplates(t *testing.T) {
50
catalog := disk.NewCatalog("")
51
52
var nilStringSlice []string
53
type args struct {
54
config *Config
55
}
56
tests := []struct {
57
name string
58
args args
59
want *Store
60
wantErr bool
61
}{
62
{
63
name: "remote-templates-positive",
64
args: args{
65
config: &Config{
66
TemplateURLs: []string{"https://raw.githubusercontent.com/projectdiscovery/nuclei-templates/main/technologies/tech-detect.yaml"},
67
RemoteTemplateDomainList: []string{"localhost", "raw.githubusercontent.com"},
68
Catalog: catalog,
69
},
70
},
71
want: &Store{
72
finalTemplates: []string{"https://raw.githubusercontent.com/projectdiscovery/nuclei-templates/main/technologies/tech-detect.yaml"},
73
},
74
wantErr: false,
75
},
76
{
77
name: "remote-templates-negative",
78
args: args{
79
config: &Config{
80
TemplateURLs: []string{"https://raw.githubusercontent.com/projectdiscovery/nuclei-templates/main/technologies/tech-detect.yaml"},
81
RemoteTemplateDomainList: []string{"localhost"},
82
Catalog: catalog,
83
},
84
},
85
want: &Store{
86
finalTemplates: nilStringSlice,
87
},
88
wantErr: true,
89
},
90
}
91
for _, tt := range tests {
92
t.Run(tt.name, func(t *testing.T) {
93
got, err := New(tt.args.config)
94
if (err != nil) != tt.wantErr {
95
t.Errorf("New() error = %v, wantErr %v", err, tt.wantErr)
96
return
97
}
98
if !reflect.DeepEqual(got.finalTemplates, tt.want.finalTemplates) {
99
t.Errorf("New() = %v, want %v", got.finalTemplates, tt.want.finalTemplates)
100
}
101
})
102
}
103
}
104
105