github.com/khulnasoft-lab/defsec@v1.0.5-0.20230827010352-5e9f46893d95/pkg/scanners/azure/arm/parser/armjson/parse_object_test.go (about) 1 package armjson 2 3 import ( 4 "testing" 5 6 "github.com/khulnasoft-lab/defsec/pkg/types" 7 8 "github.com/stretchr/testify/assert" 9 "github.com/stretchr/testify/require" 10 ) 11 12 func Test_Object(t *testing.T) { 13 example := []byte(`{ 14 "name": "testing", 15 "balance": 3.14 16 }`) 17 target := struct { 18 Name string `json:"name"` 19 Balance float64 `json:"balance"` 20 }{} 21 metadata := types.NewTestMetadata() 22 require.NoError(t, Unmarshal(example, &target, &metadata)) 23 assert.Equal(t, "testing", target.Name) 24 assert.Equal(t, 3.14, target.Balance) 25 } 26 27 func Test_ObjectWithPointers(t *testing.T) { 28 example := []byte(`{ 29 "name": "testing", 30 "balance": 3.14 31 }`) 32 target := struct { 33 Name *string `json:"name"` 34 Balance *float64 `json:"balance"` 35 }{} 36 metadata := types.NewTestMetadata() 37 require.NoError(t, Unmarshal(example, &target, &metadata)) 38 assert.Equal(t, "testing", *target.Name) 39 assert.Equal(t, 3.14, *target.Balance) 40 } 41 42 type nestedParent struct { 43 Child *nestedChild 44 Name string 45 } 46 47 type nestedChild struct { 48 Blah string `json:"secret"` 49 } 50 51 func Test_ObjectWithPointerToNestedStruct(t *testing.T) { 52 example := []byte(`{ 53 "Child": { 54 "secret": "password" 55 }, 56 "Name": "testing" 57 }`) 58 59 var parent nestedParent 60 metadata := types.NewTestMetadata() 61 require.NoError(t, Unmarshal(example, &parent, &metadata)) 62 assert.Equal(t, "testing", parent.Name) 63 assert.Equal(t, "password", parent.Child.Blah) 64 } 65 66 func Test_Object_ToMapStringInterface(t *testing.T) { 67 example := []byte(`{ 68 "Name": "testing" 69 }`) 70 71 parent := make(map[string]interface{}) 72 metadata := types.NewTestMetadata() 73 require.NoError(t, Unmarshal(example, &parent, &metadata)) 74 assert.Equal(t, "testing", parent["Name"]) 75 } 76 77 func Test_Object_ToNestedMapStringInterfaceFromIAM(t *testing.T) { 78 example := []byte(` 79 { 80 "Version": "2012-10-17", 81 "Statement": [ 82 { 83 "Sid": "", 84 "Effect": "Allow", 85 "Action": "ec2:*", 86 "Resource": "*", 87 "Condition": { 88 "Bool": { 89 "aws:MultiFactorAuthPresent": ["true"] 90 } 91 } 92 } 93 ] 94 }`) 95 96 parent := make(map[string]interface{}) 97 metadata := types.NewTestMetadata() 98 require.NoError(t, Unmarshal(example, &parent, &metadata)) 99 } 100 101 func Test_Object_ToNestedMapStringInterface(t *testing.T) { 102 example := []byte(`{ 103 "Child": { 104 "secret": "password" 105 }, 106 "Name": "testing" 107 }`) 108 109 parent := make(map[string]interface{}) 110 metadata := types.NewTestMetadata() 111 require.NoError(t, Unmarshal(example, &parent, &metadata)) 112 assert.Equal(t, "testing", parent["Name"]) 113 child := parent["Child"].(map[string]interface{}) 114 assert.Equal(t, "password", child["secret"]) 115 }