github.com/aquasecurity/trivy-iac@v0.8.1-0.20240127024015-3d8e412cf0ab/pkg/scanners/cloudformation/parser/fn_length_test.go (about)

     1  package parser
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/aquasecurity/trivy-iac/pkg/scanners/cloudformation/cftypes"
     7  	"github.com/stretchr/testify/require"
     8  )
     9  
    10  func Test_ResolveLength_WhenPropIsArray(t *testing.T) {
    11  	prop := &Property{
    12  		Inner: PropertyInner{
    13  			Type: cftypes.Map,
    14  			Value: map[string]*Property{
    15  				"Fn::Length": {
    16  					Inner: PropertyInner{
    17  						Type: cftypes.List,
    18  						Value: []*Property{
    19  							{
    20  								Inner: PropertyInner{
    21  									Type:  cftypes.Int,
    22  									Value: 1,
    23  								},
    24  							},
    25  							{
    26  								Inner: PropertyInner{
    27  									Type:  cftypes.String,
    28  									Value: "IntParameter",
    29  								},
    30  							},
    31  						},
    32  					},
    33  				},
    34  			},
    35  		},
    36  	}
    37  	resolved, ok := ResolveIntrinsicFunc(prop)
    38  	require.True(t, ok)
    39  	require.True(t, resolved.IsInt())
    40  	require.Equal(t, 2, resolved.AsInt())
    41  }
    42  
    43  func Test_ResolveLength_WhenPropIsIntrinsicFunction(t *testing.T) {
    44  	fctx := &FileContext{
    45  		Parameters: map[string]*Parameter{
    46  			"SomeParameter": {
    47  				inner: parameterInner{
    48  					Type:    "string",
    49  					Default: "a|b|c|d",
    50  				},
    51  			},
    52  		},
    53  	}
    54  	prop := &Property{
    55  		Inner: PropertyInner{
    56  			Type: cftypes.Map,
    57  			Value: map[string]*Property{
    58  				"Fn::Length": {
    59  					Inner: PropertyInner{
    60  						Type: cftypes.Map,
    61  						Value: map[string]*Property{
    62  							"Fn::Split": {
    63  								Inner: PropertyInner{
    64  									Type: cftypes.List,
    65  									Value: []*Property{
    66  										{
    67  											Inner: PropertyInner{
    68  												Type:  cftypes.String,
    69  												Value: "|",
    70  											},
    71  										},
    72  										{
    73  											ctx: fctx,
    74  											Inner: PropertyInner{
    75  												Type: cftypes.Map,
    76  												Value: map[string]*Property{
    77  													"Ref": {
    78  														Inner: PropertyInner{
    79  															Type:  cftypes.String,
    80  															Value: "SomeParameter",
    81  														},
    82  													},
    83  												},
    84  											},
    85  										},
    86  									},
    87  								},
    88  							},
    89  						},
    90  					},
    91  				},
    92  			},
    93  		},
    94  	}
    95  	resolved, ok := ResolveIntrinsicFunc(prop)
    96  	require.True(t, ok)
    97  	require.True(t, resolved.IsInt())
    98  	require.Equal(t, 4, resolved.AsInt())
    99  }