Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
projectdiscovery
GitHub Repository: projectdiscovery/nuclei
Path: blob/dev/pkg/templates/templates_test.go
2838 views
1
package templates
2
3
import (
4
"os"
5
"testing"
6
7
"github.com/projectdiscovery/nuclei/v3/pkg/utils/json"
8
"github.com/stretchr/testify/require"
9
"gopkg.in/yaml.v2"
10
)
11
12
func TestCachePoolZeroing(t *testing.T) {
13
c := NewCache()
14
15
tpl := &Template{ID: "x"}
16
raw := []byte("SOME BIG RAW")
17
18
c.Store("id1", tpl, raw, nil)
19
gotTpl, gotErr := c.Get("id1")
20
if gotErr != nil {
21
t.Fatalf("unexpected err: %v", gotErr)
22
}
23
if gotTpl == nil || gotTpl.ID != "x" {
24
t.Fatalf("unexpected tpl: %#v", gotTpl)
25
}
26
27
// StoreWithoutRaw should not retain raw
28
c.StoreWithoutRaw("id2", tpl, nil)
29
gotTpl2, gotErr2 := c.Get("id2")
30
if gotErr2 != nil {
31
t.Fatalf("unexpected err: %v", gotErr2)
32
}
33
if gotTpl2 == nil || gotTpl2.ID != "x" {
34
t.Fatalf("unexpected tpl2: %#v", gotTpl2)
35
}
36
}
37
38
func TestTemplateStruct(t *testing.T) {
39
templatePath := "./tests/match-1.yaml"
40
bin, err := os.ReadFile(templatePath)
41
require.Nil(t, err, "failed to load example template")
42
var yamlTemplate Template
43
err = yaml.Unmarshal(bin, &yamlTemplate)
44
require.Nil(t, err, "failed to unmarshal yaml template")
45
jsonBin, err := json.Marshal(yamlTemplate)
46
require.Nil(t, err, "failed to marshal template to json")
47
var jsonTemplate Template
48
err = json.Unmarshal(jsonBin, &jsonTemplate)
49
require.Nil(t, err, "failed to unmarshal json template")
50
51
templatePath = "./tests/json-template.json"
52
bin, err = os.ReadFile(templatePath)
53
require.Nil(t, err, "failed to load example template")
54
jsonTemplate = Template{}
55
err = json.Unmarshal(bin, &jsonTemplate)
56
require.Nil(t, err, "failed to unmarshal json template")
57
yamlBin, err := yaml.Marshal(jsonTemplate)
58
require.Nil(t, err, "failed to marshal template to yaml")
59
yamlTemplate = Template{}
60
err = yaml.Unmarshal(yamlBin, &yamlTemplate)
61
require.Nil(t, err, "failed to unmarshal yaml template")
62
}
63
64