Skip to content

Commit 58ded5b

Browse files
committed
fix validatePaths
1 parent 676a7e6 commit 58ded5b

File tree

2 files changed

+17
-31
lines changed

2 files changed

+17
-31
lines changed

scans_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ func TestXrayBinaryScanWithBypassArchiveLimits(t *testing.T) {
118118
// Docker scan tests
119119

120120
func TestValidatePathFunc(t *testing.T) {
121-
// ValidatePathFunc should return true for a valid path
121+
// ValidatePathFunc is made to test the validatePaths function
122122
validations.TestValidatePathsFunc(t)
123123
}
124124

utils/validations/test_validate_sca.go

Lines changed: 16 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,9 @@ func ValidateCommandJsonOutput(t *testing.T, params ValidationParams) {
4040
ValidateScanResponseFailBuild(t, params.FailBuildCVESeverity, results)
4141
}
4242
if params.ExistingProperties != nil {
43-
//TODO: TEMP Function - to use ValidatePaths
44-
ValidateContainedStrings(t, fmt.Sprintf("%v", results), params.ExistingProperties)
45-
//for _, res := range results {
46-
// ValidatePaths(t, res, params.ExistingProperties)
47-
//}
43+
for _, res := range results {
44+
ValidatePaths(t, res, params.ExistingProperties)
45+
}
4846
}
4947
}
5048
}
@@ -98,15 +96,6 @@ func ValidateScanResponses(t *testing.T, exactMatch bool, expected, actual []ser
9896
}
9997

10098
func TestValidatePathsFunc(t *testing.T) {
101-
//sampleJson := `{
102-
// "targets": [
103-
//{
104-
// "target": "/path/to/target",
105-
// "name": "some-target-name",
106-
// "technology": "some-technology",
107-
// "sca_scans": {
108-
// "xray_scan": [
109-
//{
11099
sampleJson := `{
111100
"scan_id": "1a97d1a4-4d30-430c-46e9-d0c998065d08",
112101
"vulnerabilities": [
@@ -155,19 +144,13 @@ func TestValidatePathsFunc(t *testing.T) {
155144
}
156145
]
157146
}`
158-
// ]
159-
// }
160-
// }
161-
// ]
162-
//}`
163147
actualJson := services.ScanResponse{}
164148
err := json.Unmarshal([]byte(sampleJson), &actualJson)
165149
if err != nil {
166150
assert.NoError(t, err)
167151
}
168152
stringsToCheck := []string{"vulnerabilities[].components[*].impact_paths[][].full_path"}
169153
ValidatePaths(t, actualJson, stringsToCheck)
170-
//ValidateContainedStrings(t, sampleJson, []string{"impact_paths", "full_path"})
171154
}
172155

173156
func ValidatePaths(t *testing.T, output interface{}, paths []string) {
@@ -227,8 +210,9 @@ func validatePath(data interface{}, path []string) bool {
227210
dataMap := reflect.ValueOf(data)
228211
if val := dataMap.MapIndex(reflect.ValueOf(mapKey)); val.IsValid() {
229212
if val.Kind() == reflect.Map {
230-
for _, item := range val.Interface().(map[string]interface{}) {
231-
if validatePath(item, path[1:]) {
213+
for _, item := range val.MapKeys() {
214+
mapVal := val.MapIndex(item)
215+
if validatePath(mapVal.Interface(), path[1:]) {
232216
return true
233217
}
234218
}
@@ -237,8 +221,9 @@ func validatePath(data interface{}, path []string) bool {
237221
} else if reflect.TypeOf(data).Kind() == reflect.Struct {
238222
structField, valid := getFieldByTag(data, mapKey)
239223
if valid && structField.Kind() == reflect.Map {
240-
for _, item := range reflect.ValueOf(structField.Interface()).MapKeys() {
241-
if validatePath(item, path[1:]) {
224+
for _, item := range structField.MapKeys() {
225+
mapVal := structField.MapIndex(item)
226+
if validatePath(mapVal.Interface(), path[1:]) {
242227
return true
243228
}
244229
}
@@ -256,6 +241,13 @@ func validatePath(data interface{}, path []string) bool {
256241
if valid {
257242
return validatePath(structField.Interface(), path[1:])
258243
}
244+
} else if reflect.TypeOf(data).Kind() == reflect.Slice {
245+
slice := reflect.ValueOf(data)
246+
for i := 0; i < slice.Len(); i++ {
247+
if validatePath(slice.Index(i).Interface(), path) {
248+
return true
249+
}
250+
}
259251
}
260252
}
261253
return false
@@ -286,9 +278,3 @@ func getScanResponseByScanId(scanId string, content []services.ScanResponse) *se
286278
}
287279
return nil
288280
}
289-
290-
func ValidateContainedStrings(t *testing.T, output string, strings []string) {
291-
for _, str := range strings {
292-
assert.Contains(t, output, str, "string not found: %s", str)
293-
}
294-
}

0 commit comments

Comments
 (0)