Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
projectdiscovery
GitHub Repository: projectdiscovery/nuclei
Path: blob/dev/pkg/model/model_test.go
2070 views
1
package model
2
3
import (
4
"strings"
5
"testing"
6
7
"github.com/projectdiscovery/nuclei/v3/pkg/model/types/severity"
8
"github.com/projectdiscovery/nuclei/v3/pkg/model/types/stringslice"
9
"github.com/projectdiscovery/nuclei/v3/pkg/utils/json"
10
"github.com/stretchr/testify/require"
11
"gopkg.in/yaml.v2"
12
)
13
14
func TestInfoJsonMarshal(t *testing.T) {
15
info := Info{
16
Name: "Test Template Name",
17
Authors: stringslice.StringSlice{Value: []string{"forgedhallpass", "ice3man"}},
18
Description: "Test description",
19
SeverityHolder: severity.Holder{Severity: severity.High},
20
Tags: stringslice.StringSlice{Value: []string{"cve", "misc"}},
21
Reference: stringslice.NewRawStringSlice("Reference1"),
22
Metadata: map[string]interface{}{
23
"string_key": "string_value",
24
"array_key": []string{"array_value1", "array_value2"},
25
"map_key": map[string]string{
26
"key1": "val1",
27
},
28
},
29
}
30
31
result, err := json.Marshal(&info)
32
require.Nil(t, err)
33
34
expected := `{"name":"Test Template Name","author":["forgedhallpass","ice3man"],"tags":["cve","misc"],"description":"Test description","reference":"Reference1","severity":"high","metadata":{"array_key":["array_value1","array_value2"],"map_key":{"key1":"val1"},"string_key":"string_value"}}`
35
require.Equal(t, expected, string(result))
36
}
37
38
func TestInfoYamlMarshal(t *testing.T) {
39
info := Info{
40
Name: "Test Template Name",
41
Authors: stringslice.StringSlice{Value: []string{"forgedhallpass", "ice3man"}},
42
Description: "Test description",
43
SeverityHolder: severity.Holder{Severity: severity.High},
44
Tags: stringslice.StringSlice{Value: []string{"cve", "misc"}},
45
Reference: stringslice.NewRawStringSlice("Reference1"),
46
Metadata: map[string]interface{}{
47
"string_key": "string_value",
48
"array_key": []string{"array_value1", "array_value2"},
49
"map_key": map[string]string{
50
"key1": "val1",
51
},
52
},
53
}
54
55
result, err := yaml.Marshal(&info)
56
require.Nil(t, err)
57
58
expected := `name: Test Template Name
59
author:
60
- forgedhallpass
61
- ice3man
62
tags:
63
- cve
64
- misc
65
description: Test description
66
reference: Reference1
67
severity: high
68
metadata:
69
array_key:
70
- array_value1
71
- array_value2
72
map_key:
73
key1: val1
74
string_key: string_value
75
`
76
require.Equal(t, expected, string(result))
77
}
78
79
func TestUnmarshal(t *testing.T) {
80
templateName := "Test Template"
81
authors := []string{"forgedhallpass", "ice3man"}
82
tags := []string{"cve", "misc"}
83
references := []string{"http://test.com", "http://Domain.com"}
84
85
dynamicKey1 := "customDynamicKey1"
86
dynamicKey2 := "customDynamicKey2"
87
88
dynamicKeysMap := map[string]interface{}{
89
dynamicKey1: "customDynamicValue1",
90
dynamicKey2: "customDynamicValue2",
91
}
92
93
assertUnmarshalledTemplateInfo := func(t *testing.T, yamlPayload string) Info {
94
t.Helper()
95
info := Info{}
96
err := yaml.Unmarshal([]byte(yamlPayload), &info)
97
require.Nil(t, err)
98
require.Equal(t, info.Name, templateName)
99
require.Equal(t, info.Authors.ToSlice(), authors)
100
require.Equal(t, info.Tags.ToSlice(), tags)
101
require.Equal(t, info.SeverityHolder.Severity, severity.Critical)
102
require.Equal(t, info.Reference.ToSlice(), references)
103
require.Equal(t, info.Metadata, dynamicKeysMap)
104
return info
105
}
106
107
yamlPayload1 := `
108
name: ` + templateName + `
109
author: ` + strings.Join(authors, ", ") + `
110
tags: ` + strings.Join(tags, ", ") + `
111
severity: critical
112
reference: ` + strings.Join(references, ",") + `
113
metadata:
114
` + dynamicKey1 + `: ` + dynamicKeysMap[dynamicKey1].(string) + `
115
` + dynamicKey2 + `: ` + dynamicKeysMap[dynamicKey2].(string) + `
116
`
117
yamlPayload2 := `
118
name: ` + templateName + `
119
author:
120
- ` + authors[0] + `
121
- ` + authors[1] + `
122
tags:
123
- ` + tags[0] + `
124
- ` + tags[1] + `
125
severity: critical
126
reference:
127
- ` + references[0] + ` # comments are not unmarshalled
128
- ` + references[1] + `
129
metadata:
130
` + dynamicKey1 + `: ` + dynamicKeysMap[dynamicKey1].(string) + `
131
` + dynamicKey2 + `: ` + dynamicKeysMap[dynamicKey2].(string) + `
132
`
133
134
info1 := assertUnmarshalledTemplateInfo(t, yamlPayload1)
135
info2 := assertUnmarshalledTemplateInfo(t, yamlPayload2)
136
require.Equal(t, info1, info2)
137
}
138
139