2023-01-17 07:31:20 +00:00
|
|
|
package templates
|
2023-01-17 12:50:05 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2024-04-03 11:49:06 +00:00
|
|
|
"github.com/projectdiscovery/nuclei/v3/pkg/model"
|
|
|
|
"github.com/projectdiscovery/nuclei/v3/pkg/model/types/severity"
|
2023-10-17 12:14:13 +00:00
|
|
|
"github.com/projectdiscovery/nuclei/v3/pkg/protocols/dns"
|
|
|
|
"github.com/projectdiscovery/nuclei/v3/pkg/protocols/http"
|
2024-04-03 11:49:06 +00:00
|
|
|
"github.com/projectdiscovery/nuclei/v3/pkg/testutils"
|
2023-01-17 12:50:05 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestClusterTemplates(t *testing.T) {
|
2024-04-03 11:49:06 +00:00
|
|
|
// state of whether template is flow or multiprotocol is stored in executerOptions i.e why we need to pass it
|
|
|
|
execOptions := testutils.NewMockExecuterOptions(testutils.DefaultOptions, &testutils.TemplateInfo{
|
|
|
|
ID: "templateID",
|
|
|
|
Info: model.Info{SeverityHolder: severity.Holder{Severity: severity.Low}, Name: "test"},
|
|
|
|
})
|
2023-09-13 16:57:48 +00:00
|
|
|
t.Run("http-cluster-get", func(t *testing.T) {
|
|
|
|
tp1 := &Template{Path: "first.yaml", RequestsHTTP: []*http.Request{{Path: []string{"{{BaseURL}}"}}}}
|
|
|
|
tp2 := &Template{Path: "second.yaml", RequestsHTTP: []*http.Request{{Path: []string{"{{BaseURL}}"}}}}
|
2024-04-03 11:49:06 +00:00
|
|
|
tp1.Options = execOptions
|
|
|
|
tp2.Options = execOptions
|
2023-09-13 16:57:48 +00:00
|
|
|
tpls := []*Template{tp1, tp2}
|
|
|
|
// cluster 0
|
|
|
|
expected := []*Template{tp1, tp2}
|
|
|
|
got := Cluster(tpls)[0]
|
|
|
|
require.ElementsMatchf(t, expected, got, "different %v %v", len(expected), len(got))
|
|
|
|
})
|
|
|
|
t.Run("no-http-cluster", func(t *testing.T) {
|
|
|
|
tp1 := &Template{Path: "first.yaml", RequestsHTTP: []*http.Request{{Path: []string{"{{BaseURL}}/random"}}}}
|
|
|
|
tp2 := &Template{Path: "second.yaml", RequestsHTTP: []*http.Request{{Path: []string{"{{BaseURL}}/another"}}}}
|
2024-04-03 11:49:06 +00:00
|
|
|
tp1.Options = execOptions
|
|
|
|
tp2.Options = execOptions
|
2023-09-13 16:57:48 +00:00
|
|
|
tpls := []*Template{tp1, tp2}
|
|
|
|
expected := [][]*Template{{tp1}, {tp2}}
|
|
|
|
got := Cluster(tpls)
|
|
|
|
require.ElementsMatch(t, expected, got)
|
|
|
|
})
|
|
|
|
t.Run("dns-cluster", func(t *testing.T) {
|
|
|
|
tp1 := &Template{Path: "first.yaml", RequestsDNS: []*dns.Request{{Name: "{{Hostname}}"}}}
|
|
|
|
tp2 := &Template{Path: "second.yaml", RequestsDNS: []*dns.Request{{Name: "{{Hostname}}"}}}
|
2024-04-03 11:49:06 +00:00
|
|
|
tp1.Options = execOptions
|
|
|
|
tp2.Options = execOptions
|
2023-09-13 16:57:48 +00:00
|
|
|
tpls := []*Template{tp1, tp2}
|
|
|
|
// cluster 0
|
|
|
|
expected := []*Template{tp1, tp2}
|
|
|
|
got := Cluster(tpls)[0]
|
|
|
|
require.ElementsMatch(t, got, expected)
|
|
|
|
})
|
2023-01-17 12:50:05 +00:00
|
|
|
}
|