github.com/aquasecurity/trivy-iac@v0.8.1-0.20240127024015-3d8e412cf0ab/pkg/scanners/azure/arm/parser/armjson/parse_complex_test.go (about) 1 package armjson 2 3 import ( 4 "testing" 5 6 "github.com/aquasecurity/defsec/pkg/types" 7 8 "github.com/stretchr/testify/require" 9 ) 10 11 func Test_Complex(t *testing.T) { 12 target := make(map[string]interface{}) 13 input := `{ 14 "glossary": { 15 "title": "example glossary", 16 "GlossDiv": { 17 "title": "S", 18 "GlossList": { 19 "GlossEntry": { 20 "ID": "SGML", 21 "SortAs": "SGML", 22 "GlossTerm": "Standard Generalized Markup Language", 23 "Acronym": "SGML", 24 "Abbrev": "ISO 8879:1986", 25 "GlossDef": { 26 "para": "A meta-markup language, used to create markup languages such as DocBook.", 27 "GlossSeeAlso": ["GML", "XML"] 28 }, 29 "GlossSee": "markup" 30 } 31 } 32 } 33 } 34 }` 35 metadata := types.NewTestMetadata() 36 require.NoError(t, Unmarshal([]byte(input), &target, &metadata)) 37 } 38 39 type Resource struct { 40 Line int 41 inner resourceInner 42 } 43 44 type resourceInner struct { 45 Type string `json:"Type" yaml:"Type"` 46 Properties map[string]*Property `json:"Properties" yaml:"Properties"` 47 } 48 49 func (r *Resource) UnmarshalJSONWithMetadata(node Node) error { 50 r.Line = node.Range().Start.Line 51 return node.Decode(&r.inner) 52 } 53 54 type Parameter struct { 55 inner parameterInner 56 } 57 58 type parameterInner struct { 59 Type string `json:"Type" yaml:"Type"` 60 Default interface{} `yaml:"Default"` 61 } 62 63 func (p *Parameter) UnmarshalJSONWithMetadata(node Node) error { 64 return node.Decode(&p.inner) 65 } 66 67 type Property struct { 68 Line int 69 inner propertyInner 70 } 71 72 type CFType string 73 74 type propertyInner struct { 75 Type CFType 76 Value interface{} `json:"Value" yaml:"Value"` 77 } 78 79 func (p *Property) UnmarshalJSONWithMetadata(node Node) error { 80 p.Line = node.Range().Start.Line 81 return node.Decode(&p.inner) 82 } 83 84 type Temp struct { 85 BucketName *Parameter 86 BucketKeyEnabled *Parameter 87 } 88 89 type FileContext struct { 90 Parameters map[string]*Parameter `json:"Parameters" yaml:"Parameters"` 91 Resources map[string]*Resource `json:"Resources" yaml:"Resources"` 92 } 93 94 func Test_CloudFormation(t *testing.T) { 95 var target FileContext 96 input := ` 97 { 98 "Parameters": { 99 "BucketName": { 100 "Type": "String", 101 "Default": "naughty" 102 }, 103 "BucketKeyEnabled": { 104 "Type": "Boolean", 105 "Default": false 106 } 107 }, 108 "Resources": { 109 "S3Bucket": { 110 "Type": "AWS::S3::Bucket", 111 "Properties": { 112 "BucketName": { 113 "Ref": "BucketName" 114 }, 115 "BucketEncryption": { 116 "ServerSideEncryptionConfiguration": [ 117 { 118 "BucketKeyEnabled": { 119 "Ref": "BucketKeyEnabled" 120 } 121 } 122 ] 123 } 124 } 125 } 126 } 127 } 128 ` 129 metadata := types.NewTestMetadata() 130 require.NoError(t, Unmarshal([]byte(input), &target, &metadata)) 131 }