github.com/khulnasoft-lab/defsec@v1.0.5-0.20230827010352-5e9f46893d95/rules/cloud/policies/nifcloud/nas/no_common_private_nas_instance_test.go (about) 1 package nas 2 3 import ( 4 "testing" 5 6 "github.com/khulnasoft-lab/defsec/pkg/providers/nifcloud/nas" 7 defsecTypes "github.com/khulnasoft-lab/defsec/pkg/types" 8 9 "github.com/khulnasoft-lab/defsec/pkg/scan" 10 11 "github.com/khulnasoft-lab/defsec/pkg/state" 12 13 "github.com/stretchr/testify/assert" 14 ) 15 16 func TestCheckNoCommonPrivateNASInstance(t *testing.T) { 17 tests := []struct { 18 name string 19 input nas.NAS 20 expected bool 21 }{ 22 { 23 name: "NIFCLOUD nas instance with common private", 24 input: nas.NAS{ 25 NASInstances: []nas.NASInstance{ 26 { 27 Metadata: defsecTypes.NewTestMetadata(), 28 NetworkID: defsecTypes.String("net-COMMON_PRIVATE", defsecTypes.NewTestMetadata()), 29 }, 30 }, 31 }, 32 expected: true, 33 }, 34 { 35 name: "NIFCLOUD nas instance with private LAN", 36 input: nas.NAS{ 37 NASInstances: []nas.NASInstance{ 38 { 39 Metadata: defsecTypes.NewTestMetadata(), 40 NetworkID: defsecTypes.String("net-some-private-lan", defsecTypes.NewTestMetadata()), 41 }, 42 }, 43 }, 44 expected: false, 45 }, 46 } 47 for _, test := range tests { 48 t.Run(test.name, func(t *testing.T) { 49 var testState state.State 50 testState.Nifcloud.NAS = test.input 51 results := CheckNoCommonPrivateNASInstance.Evaluate(&testState) 52 var found bool 53 for _, result := range results { 54 if result.Status() == scan.StatusFailed && result.Rule().LongID() == CheckNoCommonPrivateNASInstance.Rule().LongID() { 55 found = true 56 } 57 } 58 if test.expected { 59 assert.True(t, found, "Rule should have been found") 60 } else { 61 assert.False(t, found, "Rule should not have been found") 62 } 63 }) 64 } 65 }