-
Notifications
You must be signed in to change notification settings - Fork 311
/
html.go
193 lines (157 loc) · 6.41 KB
/
html.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package utils
import (
"os"
"path/filepath"
"strconv"
"strings"
"testing"
"github.com/Checkmarx/kics/internal/constants"
"github.com/stretchr/testify/require"
"golang.org/x/net/html"
)
var availablePlatforms = initPlatforms()
func initPlatforms() map[string]string {
platforms := make(map[string]string)
for k, v := range constants.AvailablePlatforms {
platforms[k] = v
}
platforms["Common"] = "common"
return platforms
}
// HTMLValidation executes many asserts to validate the HTML Report
func HTMLValidation(t *testing.T, file string) {
// Read & Parse Expected HTML Report
expectHTMLString, errExpStr := os.ReadFile(filepath.Join("fixtures", file))
require.NoError(t, errExpStr, "Openning Expected HTML File should not yield an error")
expectedHTML, errExp := html.Parse(strings.NewReader(string(expectHTMLString)))
require.NoError(t, errExp, "Openning Expected HTML File should not yield an error")
// Read & Parse Output HTML Report
actualHTMLString, errActStr := os.ReadFile(filepath.Join("output", file))
require.NoError(t, errActStr, "Openning Actual HTML File should not yield an error")
actualHTML, errAct := html.Parse(strings.NewReader(string(actualHTMLString)))
require.NoError(t, errAct, "Openning Actual HTML File should not yield an error")
// Compare Header Data (Paths, Platforms)
headerIds := []string{"scan-paths", "scan-platforms"}
for arg := range headerIds {
expectedValue := getElementByID(expectedHTML, headerIds[arg])
actualValue := getElementByID(actualHTML, headerIds[arg])
// Adapt path if running locally (dev)
if GetKICSDockerImageName() == "" {
expectedValue.LastChild.Data = KicsDevPathAdapter(expectedValue.LastChild.Data)
}
require.NotNil(t, actualValue.LastChild,
"[%s] Invalid value in Element ID <%s>", file, headerIds[arg])
require.Equal(t, expectedValue.LastChild.Data, actualValue.LastChild.Data,
"[%s] HTML Element <%s>:\n- Expected value: %s\n- Actual value: %s\n",
file, headerIds[arg], expectedValue.LastChild.Data, actualValue.LastChild.Data)
}
// Compare Severity Values (High, Medium, Total...)
severityIds := []string{"info", "low", "medium", "high", "total"}
for arg := range severityIds {
nodeIdentificator := "severity-count-" + severityIds[arg]
expectedSeverityValue := getElementByID(expectedHTML, nodeIdentificator)
actualSeverityValue := getElementByID(actualHTML, nodeIdentificator)
require.NotNil(t, actualSeverityValue.FirstChild,
"[%s] Invalid value in Element ID <%s>", file, nodeIdentificator)
require.Equal(t, expectedSeverityValue.FirstChild.Data, actualSeverityValue.FirstChild.Data,
"[%s] HTML Element <%s>:\n- Expected value: %s\n- Actual value: %s\n",
file, nodeIdentificator, expectedSeverityValue.FirstChild.Data, actualSeverityValue.FirstChild.Data)
classIdentificator := "severity-partial-count-" + severityIds[arg]
expectedSeverityClassValues := getAndSumElementsByClass(expectedHTML, classIdentificator)
actualSeverityClassValues := getAndSumElementsByClass(actualHTML, classIdentificator)
require.Equal(t, expectedSeverityClassValues, actualSeverityClassValues,
"[%s] Expected Sum of HTML classes <%s>:\n- Expected Value: %d\n- Actual value: %d\n",
file, classIdentificator, expectedSeverityClassValues, actualSeverityClassValues)
}
// Validate Query Names
queriesClassname := "query-name"
queriesClasses := getElementsByClass(actualHTML, queriesClassname)
for _, node := range queriesClasses {
require.NotNil(t, node.FirstChild,
"[%s] Invalid query in class element <%s>", file, queriesClassname)
}
// Validate Platforms
platformClassname := "query-info-platform"
platformsClasses := getElementsByClass(actualHTML, platformClassname)
for _, node := range platformsClasses {
require.NotNil(t, node.FirstChild,
"[%s] Invalid plataform in class element <%s>", file, platformClassname)
require.NotEmpty(t, availablePlatforms[node.FirstChild.Data],
"[%s] Invalid plataform in class element <%s>: %s\n", file, platformClassname, node.FirstChild.Data)
}
// Validate Categories
categoriesClassname := "query-info-category"
categoriesClasses := getElementsByClass(actualHTML, categoriesClassname)
for _, node := range categoriesClasses {
require.NotNil(t, node.FirstChild,
"[%s] Invalid category in class element <%s>", file, categoriesClassname)
require.NotEmpty(t, constants.AvailableCategories[node.FirstChild.Data],
"[%s] Invalid category in class element <%s>: %s\n", file, categoriesClassname, node.FirstChild.Data)
}
// Validate Total Number of Results and Code-Boxes
totalClassname := "severity-count-total"
total, err := strconv.Atoi(getElementByID(actualHTML, totalClassname).FirstChild.Data)
require.NoError(t, err, "Getting Total Results should not yield an error")
codeBoxClassname := "code-box"
codeBoxClasses := getElementsByClass(actualHTML, codeBoxClassname)
require.Equal(t, total, len(codeBoxClasses),
"[%s] The Value of Element ID <%s> is not equal the number of <%s> classes in the HTML File"+
"\n- <%s>: %d\n- <%s>: %d\n",
file, totalClassname, codeBoxClassname, totalClassname, total, codeBoxClassname, len(codeBoxClasses))
}
func findAttribute(node *html.Node, key string) (string, bool) {
for _, attr := range node.Attr {
if attr.Key == key {
return attr.Val, true
}
}
return "", false
}
func existsAttribute(node *html.Node, name, tag string) bool {
if node.Type == html.ElementNode {
value, exists := findAttribute(node, tag)
if exists && value == name {
return true
}
}
return false
}
func getElementByID(n *html.Node, name string) *html.Node {
response := n
var f func(node *html.Node, name string)
f = func(node *html.Node, name string) {
if existsAttribute(node, name, "id") {
response = node
}
for c := node.FirstChild; c != nil; c = c.NextSibling {
f(c, name)
}
}
f(n, name)
return response
}
func getElementsByClass(n *html.Node, name string) []*html.Node {
classNodes := []*html.Node{}
var f func(node *html.Node, name string)
f = func(node *html.Node, name string) {
if existsAttribute(node, name, "class") {
classNodes = append(classNodes, node)
}
for c := node.FirstChild; c != nil; c = c.NextSibling {
f(c, name)
}
}
f(n, name)
return classNodes
}
func getAndSumElementsByClass(n *html.Node, name string) int {
classes := getElementsByClass(n, name)
result := 0
for i := range classes {
classValue, err := strconv.Atoi(classes[i].FirstChild.Data)
if err == nil {
result += classValue
}
}
return result
}