github.com/aquasecurity/trivy-iac@v0.8.1-0.20240127024015-3d8e412cf0ab/pkg/scanners/cloudformation/parser/fn_join_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_join_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::Join": {
    24  					Inner: PropertyInner{
    25  						Type: cftypes.List,
    26  						Value: []*Property{
    27  							{
    28  								Inner: PropertyInner{
    29  									Type:  cftypes.String,
    30  									Value: "::",
    31  								},
    32  							},
    33  							{
    34  								Inner: PropertyInner{
    35  									Type: cftypes.List,
    36  									Value: []*Property{
    37  										{
    38  											Inner: PropertyInner{
    39  												Type:  cftypes.String,
    40  												Value: "s3",
    41  											},
    42  										},
    43  										{
    44  											Inner: PropertyInner{
    45  												Type:  cftypes.String,
    46  												Value: "part1",
    47  											},
    48  										},
    49  										{
    50  											Inner: PropertyInner{
    51  												Type:  cftypes.String,
    52  												Value: "part2",
    53  											},
    54  										},
    55  									},
    56  								},
    57  							},
    58  						},
    59  					},
    60  				},
    61  			},
    62  		},
    63  	}
    64  	resolvedProperty, success := ResolveIntrinsicFunc(property)
    65  	require.True(t, success)
    66  
    67  	assert.Equal(t, "s3::part1::part2", resolvedProperty.AsString())
    68  }
    69  
    70  func Test_resolve_join_value_with_reference(t *testing.T) {
    71  
    72  	property := &Property{
    73  		ctx: &FileContext{
    74  			filepath: "",
    75  			Parameters: map[string]*Parameter{
    76  				"Environment": {
    77  					inner: parameterInner{
    78  						Type:    "string",
    79  						Default: "staging",
    80  					},
    81  				},
    82  			},
    83  		},
    84  		name: "EnvironmentBucket",
    85  		rng:  types.NewRange("testfile", 1, 1, "", nil),
    86  		Inner: PropertyInner{
    87  			Type: cftypes.Map,
    88  			Value: map[string]*Property{
    89  				"Fn::Join": {
    90  					Inner: PropertyInner{
    91  						Type: cftypes.List,
    92  						Value: []*Property{
    93  							{
    94  								Inner: PropertyInner{
    95  									Type:  cftypes.String,
    96  									Value: "::",
    97  								},
    98  							},
    99  							{
   100  								Inner: PropertyInner{
   101  									Type: cftypes.List,
   102  									Value: []*Property{
   103  										{
   104  											Inner: PropertyInner{
   105  												Type:  cftypes.String,
   106  												Value: "s3",
   107  											},
   108  										},
   109  										{
   110  											Inner: PropertyInner{
   111  												Type:  cftypes.String,
   112  												Value: "part1",
   113  											},
   114  										},
   115  										{
   116  											ctx: &FileContext{
   117  												filepath: "",
   118  												Parameters: map[string]*Parameter{
   119  													"Environment": {
   120  														inner: parameterInner{
   121  															Type:    "string",
   122  															Default: "staging",
   123  														},
   124  													},
   125  												},
   126  											},
   127  											Inner: PropertyInner{
   128  												Type: cftypes.Map,
   129  												Value: map[string]*Property{
   130  													"Ref": {
   131  														Inner: PropertyInner{
   132  															Type:  cftypes.String,
   133  															Value: "Environment",
   134  														},
   135  													},
   136  												},
   137  											},
   138  										},
   139  									},
   140  								},
   141  							},
   142  						},
   143  					},
   144  				},
   145  			},
   146  		},
   147  	}
   148  	resolvedProperty, success := ResolveIntrinsicFunc(property)
   149  	require.True(t, success)
   150  
   151  	assert.Equal(t, "s3::part1::staging", resolvedProperty.AsString())
   152  }