Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
projectdiscovery
GitHub Repository: projectdiscovery/nuclei
Path: blob/dev/pkg/templates/cluster_test.go
2070 views
1
package templates
2
3
import (
4
"testing"
5
6
"github.com/projectdiscovery/nuclei/v3/pkg/model"
7
"github.com/projectdiscovery/nuclei/v3/pkg/model/types/severity"
8
"github.com/projectdiscovery/nuclei/v3/pkg/protocols/dns"
9
"github.com/projectdiscovery/nuclei/v3/pkg/protocols/http"
10
"github.com/projectdiscovery/nuclei/v3/pkg/testutils"
11
"github.com/stretchr/testify/require"
12
)
13
14
func TestClusterTemplates(t *testing.T) {
15
// state of whether template is flow or multiprotocol is stored in executerOptions i.e why we need to pass it
16
execOptions := testutils.NewMockExecuterOptions(testutils.DefaultOptions, &testutils.TemplateInfo{
17
ID: "templateID",
18
Info: model.Info{SeverityHolder: severity.Holder{Severity: severity.Low}, Name: "test"},
19
})
20
t.Run("http-cluster-get", func(t *testing.T) {
21
tp1 := &Template{Path: "first.yaml", RequestsHTTP: []*http.Request{{Path: []string{"{{BaseURL}}"}}}}
22
tp2 := &Template{Path: "second.yaml", RequestsHTTP: []*http.Request{{Path: []string{"{{BaseURL}}"}}}}
23
tp1.Options = execOptions
24
tp2.Options = execOptions
25
tpls := []*Template{tp1, tp2}
26
// cluster 0
27
expected := []*Template{tp1, tp2}
28
got := Cluster(tpls)[0]
29
require.ElementsMatchf(t, expected, got, "different %v %v", len(expected), len(got))
30
})
31
t.Run("no-http-cluster", func(t *testing.T) {
32
tp1 := &Template{Path: "first.yaml", RequestsHTTP: []*http.Request{{Path: []string{"{{BaseURL}}/random"}}}}
33
tp2 := &Template{Path: "second.yaml", RequestsHTTP: []*http.Request{{Path: []string{"{{BaseURL}}/another"}}}}
34
tp1.Options = execOptions
35
tp2.Options = execOptions
36
tpls := []*Template{tp1, tp2}
37
expected := [][]*Template{{tp1}, {tp2}}
38
got := Cluster(tpls)
39
require.ElementsMatch(t, expected, got)
40
})
41
t.Run("dns-cluster", func(t *testing.T) {
42
tp1 := &Template{Path: "first.yaml", RequestsDNS: []*dns.Request{{Name: "{{Hostname}}"}}}
43
tp2 := &Template{Path: "second.yaml", RequestsDNS: []*dns.Request{{Name: "{{Hostname}}"}}}
44
tp1.Options = execOptions
45
tp2.Options = execOptions
46
tpls := []*Template{tp1, tp2}
47
// cluster 0
48
expected := []*Template{tp1, tp2}
49
got := Cluster(tpls)[0]
50
require.ElementsMatch(t, got, expected)
51
})
52
}
53
54