github.com/aquasecurity/trivy-iac@v0.8.1-0.20240127024015-3d8e412cf0ab/pkg/scanners/cloudformation/parser/property_helpers_test.go (about) 1 package parser 2 3 import ( 4 "testing" 5 6 "github.com/aquasecurity/defsec/pkg/types" 7 "github.com/aquasecurity/trivy-iac/pkg/scanners/cloudformation/cftypes" 8 "github.com/stretchr/testify/assert" 9 ) 10 11 func newProp(inner PropertyInner) *Property { 12 return &Property{ 13 name: "test_prop", 14 ctx: &FileContext{}, 15 rng: types.NewRange("testfile", 1, 1, "", nil), 16 Inner: inner, 17 } 18 } 19 20 func Test_EqualTo(t *testing.T) { 21 tests := []struct { 22 name string 23 property *Property 24 checkValue interface{} 25 opts []EqualityOptions 26 isEqual bool 27 }{ 28 { 29 name: "prop is nil", 30 property: nil, 31 checkValue: "some value", 32 isEqual: false, 33 }, 34 { 35 name: "compare strings", 36 property: newProp(PropertyInner{ 37 Type: cftypes.String, 38 Value: "is str", 39 }), 40 checkValue: "is str", 41 isEqual: true, 42 }, 43 { 44 name: "compare strings ignoring case", 45 property: newProp(PropertyInner{ 46 Type: cftypes.String, 47 Value: "is str", 48 }), 49 opts: []EqualityOptions{IgnoreCase}, 50 checkValue: "Is StR", 51 isEqual: true, 52 }, 53 { 54 name: "strings ate not equal", 55 property: newProp(PropertyInner{ 56 Type: cftypes.String, 57 Value: "some value", 58 }), 59 checkValue: "some other value", 60 isEqual: false, 61 }, 62 { 63 name: "compare prop with a int represented by a string", 64 property: newProp(PropertyInner{ 65 Type: cftypes.Int, 66 Value: 147, 67 }), 68 checkValue: "147", 69 isEqual: true, 70 }, 71 { 72 name: "compare ints", 73 property: newProp(PropertyInner{ 74 Type: cftypes.Int, 75 Value: 701, 76 }), 77 checkValue: 701, 78 isEqual: true, 79 }, 80 { 81 name: "compare bools", 82 property: newProp(PropertyInner{ 83 Type: cftypes.Bool, 84 Value: true, 85 }), 86 checkValue: true, 87 isEqual: true, 88 }, 89 { 90 name: "prop is string fn", 91 property: newProp(PropertyInner{ 92 Type: cftypes.Map, 93 Value: map[string]*Property{ 94 "Fn::If": { 95 Inner: PropertyInner{ 96 Type: cftypes.List, 97 Value: []*Property{ 98 { 99 Inner: PropertyInner{ 100 Type: cftypes.Bool, 101 Value: false, 102 }, 103 }, 104 { 105 Inner: PropertyInner{ 106 Type: cftypes.String, 107 Value: "bad", 108 }, 109 }, 110 { 111 Inner: PropertyInner{ 112 Type: cftypes.String, 113 Value: "some value", 114 }, 115 }, 116 }, 117 }, 118 }, 119 }, 120 }), 121 checkValue: "some value", 122 isEqual: true, 123 }, 124 { 125 name: "prop is int fn", 126 property: newProp(PropertyInner{ 127 Type: cftypes.Map, 128 Value: map[string]*Property{ 129 "Fn::If": { 130 Inner: PropertyInner{ 131 Type: cftypes.List, 132 Value: []*Property{ 133 { 134 Inner: PropertyInner{ 135 Type: cftypes.Bool, 136 Value: true, 137 }, 138 }, 139 { 140 Inner: PropertyInner{ 141 Type: cftypes.Int, 142 Value: 121, 143 }, 144 }, 145 { 146 Inner: PropertyInner{ 147 Type: cftypes.Int, 148 Value: -1, 149 }, 150 }, 151 }, 152 }, 153 }, 154 }, 155 }), 156 checkValue: 121, 157 isEqual: true, 158 }, 159 { 160 name: "prop is bool fn", 161 property: newProp(PropertyInner{ 162 Type: cftypes.Map, 163 Value: map[string]*Property{ 164 "Fn::Equals": { 165 Inner: PropertyInner{ 166 Type: cftypes.List, 167 Value: []*Property{ 168 { 169 Inner: PropertyInner{ 170 Type: cftypes.String, 171 Value: "foo", 172 }, 173 }, 174 { 175 Inner: PropertyInner{ 176 Type: cftypes.String, 177 Value: "foo", 178 }, 179 }, 180 }, 181 }, 182 }, 183 }, 184 }), 185 checkValue: true, 186 isEqual: true, 187 }, 188 } 189 190 for _, tt := range tests { 191 t.Run(tt.name, func(t *testing.T) { 192 assert.Equal(t, tt.isEqual, tt.property.EqualTo(tt.checkValue, tt.opts...)) 193 }) 194 } 195 }