-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(cloudformation): evaluate the value for a property when comparing (…
…#1393) fix(misconf): evaluate the value for a property when comparing
- Loading branch information
Showing
2 changed files
with
213 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
195 changes: 195 additions & 0 deletions
195
pkg/scanners/cloudformation/parser/property_helpers_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,195 @@ | ||
package parser | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/aquasecurity/defsec/pkg/scanners/cloudformation/cftypes" | ||
"github.com/aquasecurity/defsec/pkg/types" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func newProp(inner PropertyInner) *Property { | ||
return &Property{ | ||
name: "test_prop", | ||
ctx: &FileContext{}, | ||
rng: types.NewRange("testfile", 1, 1, "", nil), | ||
Inner: inner, | ||
} | ||
} | ||
|
||
func Test_EqualTo(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
property *Property | ||
checkValue interface{} | ||
opts []EqualityOptions | ||
isEqual bool | ||
}{ | ||
{ | ||
name: "prop is nil", | ||
property: nil, | ||
checkValue: "some value", | ||
isEqual: false, | ||
}, | ||
{ | ||
name: "compare strings", | ||
property: newProp(PropertyInner{ | ||
Type: cftypes.String, | ||
Value: "is str", | ||
}), | ||
checkValue: "is str", | ||
isEqual: true, | ||
}, | ||
{ | ||
name: "compare strings ignoring case", | ||
property: newProp(PropertyInner{ | ||
Type: cftypes.String, | ||
Value: "is str", | ||
}), | ||
opts: []EqualityOptions{IgnoreCase}, | ||
checkValue: "Is StR", | ||
isEqual: true, | ||
}, | ||
{ | ||
name: "strings ate not equal", | ||
property: newProp(PropertyInner{ | ||
Type: cftypes.String, | ||
Value: "some value", | ||
}), | ||
checkValue: "some other value", | ||
isEqual: false, | ||
}, | ||
{ | ||
name: "compare prop with a int represented by a string", | ||
property: newProp(PropertyInner{ | ||
Type: cftypes.Int, | ||
Value: 147, | ||
}), | ||
checkValue: "147", | ||
isEqual: true, | ||
}, | ||
{ | ||
name: "compare ints", | ||
property: newProp(PropertyInner{ | ||
Type: cftypes.Int, | ||
Value: 701, | ||
}), | ||
checkValue: 701, | ||
isEqual: true, | ||
}, | ||
{ | ||
name: "compare bools", | ||
property: newProp(PropertyInner{ | ||
Type: cftypes.Bool, | ||
Value: true, | ||
}), | ||
checkValue: true, | ||
isEqual: true, | ||
}, | ||
{ | ||
name: "prop is string fn", | ||
property: newProp(PropertyInner{ | ||
Type: cftypes.Map, | ||
Value: map[string]*Property{ | ||
"Fn::If": { | ||
Inner: PropertyInner{ | ||
Type: cftypes.List, | ||
Value: []*Property{ | ||
{ | ||
Inner: PropertyInner{ | ||
Type: cftypes.Bool, | ||
Value: false, | ||
}, | ||
}, | ||
{ | ||
Inner: PropertyInner{ | ||
Type: cftypes.String, | ||
Value: "bad", | ||
}, | ||
}, | ||
{ | ||
Inner: PropertyInner{ | ||
Type: cftypes.String, | ||
Value: "some value", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}), | ||
checkValue: "some value", | ||
isEqual: true, | ||
}, | ||
{ | ||
name: "prop is int fn", | ||
property: newProp(PropertyInner{ | ||
Type: cftypes.Map, | ||
Value: map[string]*Property{ | ||
"Fn::If": { | ||
Inner: PropertyInner{ | ||
Type: cftypes.List, | ||
Value: []*Property{ | ||
{ | ||
Inner: PropertyInner{ | ||
Type: cftypes.Bool, | ||
Value: true, | ||
}, | ||
}, | ||
{ | ||
Inner: PropertyInner{ | ||
Type: cftypes.Int, | ||
Value: 121, | ||
}, | ||
}, | ||
{ | ||
Inner: PropertyInner{ | ||
Type: cftypes.Int, | ||
Value: -1, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}), | ||
checkValue: 121, | ||
isEqual: true, | ||
}, | ||
{ | ||
name: "prop is bool fn", | ||
property: newProp(PropertyInner{ | ||
Type: cftypes.Map, | ||
Value: map[string]*Property{ | ||
"Fn::Equals": { | ||
Inner: PropertyInner{ | ||
Type: cftypes.List, | ||
Value: []*Property{ | ||
{ | ||
Inner: PropertyInner{ | ||
Type: cftypes.String, | ||
Value: "foo", | ||
}, | ||
}, | ||
{ | ||
Inner: PropertyInner{ | ||
Type: cftypes.String, | ||
Value: "foo", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}), | ||
checkValue: true, | ||
isEqual: true, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
assert.Equal(t, tt.isEqual, tt.property.EqualTo(tt.checkValue, tt.opts...)) | ||
}) | ||
} | ||
} |