github.com/aquasecurity/trivy-iac@v0.8.1-0.20240127024015-3d8e412cf0ab/pkg/scanners/cloudformation/parser/fn_equals_test.go (about) 1 package parser 2 3 import ( 4 "testing" 5 6 "github.com/aquasecurity/defsec/pkg/types" 7 8 "github.com/aquasecurity/trivy-iac/pkg/scanners/cloudformation/cftypes" 9 10 "github.com/stretchr/testify/assert" 11 "github.com/stretchr/testify/require" 12 ) 13 14 func Test_resolve_equals_value(t *testing.T) { 15 16 property := &Property{ 17 ctx: &FileContext{}, 18 name: "BucketName", 19 rng: types.NewRange("testfile", 1, 1, "", nil), 20 Inner: PropertyInner{ 21 Type: cftypes.Map, 22 Value: map[string]*Property{ 23 "Fn::Equals": { 24 Inner: PropertyInner{ 25 Type: cftypes.List, 26 Value: []*Property{ 27 { 28 Inner: PropertyInner{ 29 Type: cftypes.String, 30 Value: "foo", 31 }, 32 }, 33 { 34 Inner: PropertyInner{ 35 Type: cftypes.String, 36 Value: "foo", 37 }, 38 }, 39 }, 40 }, 41 }, 42 }, 43 }, 44 } 45 46 resolvedProperty, success := ResolveIntrinsicFunc(property) 47 require.True(t, success) 48 49 assert.True(t, resolvedProperty.IsTrue()) 50 } 51 52 func Test_resolve_equals_value_to_false(t *testing.T) { 53 54 property := &Property{ 55 ctx: &FileContext{}, 56 name: "BucketName", 57 rng: types.NewRange("testfile", 1, 1, "", nil), 58 Inner: PropertyInner{ 59 Type: cftypes.Map, 60 Value: map[string]*Property{ 61 "Fn::Equals": { 62 Inner: PropertyInner{ 63 Type: cftypes.List, 64 Value: []*Property{ 65 { 66 Inner: PropertyInner{ 67 Type: cftypes.String, 68 Value: "foo", 69 }, 70 }, 71 { 72 Inner: PropertyInner{ 73 Type: cftypes.String, 74 Value: "bar", 75 }, 76 }, 77 }, 78 }, 79 }, 80 }, 81 }, 82 } 83 84 resolvedProperty, success := ResolveIntrinsicFunc(property) 85 require.True(t, success) 86 87 assert.False(t, resolvedProperty.IsTrue()) 88 } 89 90 func Test_resolve_equals_value_to_true_when_boolean(t *testing.T) { 91 92 property := &Property{ 93 ctx: &FileContext{}, 94 name: "BucketName", 95 rng: types.NewRange("testfile", 1, 1, "", nil), 96 Inner: PropertyInner{ 97 Type: cftypes.Map, 98 Value: map[string]*Property{ 99 "Fn::Equals": { 100 Inner: PropertyInner{ 101 Type: cftypes.List, 102 Value: []*Property{ 103 { 104 Inner: PropertyInner{ 105 Type: cftypes.Bool, 106 Value: true, 107 }, 108 }, 109 { 110 Inner: PropertyInner{ 111 Type: cftypes.Bool, 112 Value: true, 113 }, 114 }, 115 }, 116 }, 117 }, 118 }, 119 }, 120 } 121 122 resolvedProperty, success := ResolveIntrinsicFunc(property) 123 require.True(t, success) 124 assert.True(t, resolvedProperty.IsTrue()) 125 } 126 127 func Test_resolve_equals_value_when_one_is_a_reference(t *testing.T) { 128 129 property := &Property{ 130 name: "BucketName", 131 rng: types.NewRange("testfile", 1, 1, "", nil), 132 Inner: PropertyInner{ 133 Type: cftypes.Map, 134 Value: map[string]*Property{ 135 "Fn::Equals": { 136 Inner: PropertyInner{ 137 Type: cftypes.List, 138 Value: []*Property{ 139 { 140 Inner: PropertyInner{ 141 Type: cftypes.String, 142 Value: "staging", 143 }, 144 }, 145 { 146 ctx: &FileContext{ 147 filepath: "", 148 Parameters: map[string]*Parameter{ 149 "Environment": { 150 inner: parameterInner{ 151 Type: "string", 152 Default: "staging", 153 }, 154 }, 155 }, 156 }, 157 Inner: PropertyInner{ 158 Type: cftypes.Map, 159 Value: map[string]*Property{ 160 "Ref": { 161 Inner: PropertyInner{ 162 Type: cftypes.String, 163 Value: "Environment", 164 }, 165 }, 166 }, 167 }, 168 }, 169 }, 170 }, 171 }, 172 }, 173 }, 174 } 175 176 resolvedProperty, success := ResolveIntrinsicFunc(property) 177 require.True(t, success) 178 179 assert.True(t, resolvedProperty.IsTrue()) 180 }