forked from litmuschaos/litmus-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: Adding fuzz testing for common util (litmuschaos#688)
Signed-off-by: Shubham Chaudhary <[email protected]>
- Loading branch information
1 parent
eef3b40
commit d72c7f0
Showing
5 changed files
with
141 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
package common | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
|
||
fuzz "github.com/AdaLogics/go-fuzz-headers" | ||
"github.com/litmuschaos/litmus-go/pkg/types" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func FuzzGetContainerNames(f *testing.F) { | ||
|
||
f.Fuzz(func(t *testing.T, data []byte) { | ||
fuzzConsumer := fuzz.NewConsumer(data) | ||
targetStruct := &struct { | ||
chaosDetails types.ChaosDetails | ||
}{} | ||
err := fuzzConsumer.GenerateStruct(targetStruct) | ||
if err != nil { | ||
return | ||
} | ||
names := GetContainerNames(&targetStruct.chaosDetails) | ||
require.Equal(t, len(names), len(targetStruct.chaosDetails.SideCar)+1) | ||
}) | ||
} | ||
|
||
func FuzzGetSidecarVolumes(f *testing.F) { | ||
|
||
f.Fuzz(func(t *testing.T, data []byte) { | ||
fuzzConsumer := fuzz.NewConsumer(data) | ||
targetStruct := &struct { | ||
chaosDetails types.ChaosDetails | ||
}{} | ||
err := fuzzConsumer.GenerateStruct(targetStruct) | ||
if err != nil { | ||
return | ||
} | ||
volumes := GetSidecarVolumes(&targetStruct.chaosDetails) | ||
var volCounts = 0 | ||
for _, s := range targetStruct.chaosDetails.SideCar { | ||
volCounts += len(s.Secrets) | ||
} | ||
require.Equal(t, len(volumes), len(volumes)) | ||
}) | ||
} | ||
|
||
func FuzzBuildSidecar(f *testing.F) { | ||
|
||
f.Fuzz(func(t *testing.T, data []byte) { | ||
fuzzConsumer := fuzz.NewConsumer(data) | ||
targetStruct := &struct { | ||
chaosDetails types.ChaosDetails | ||
}{} | ||
err := fuzzConsumer.GenerateStruct(targetStruct) | ||
if err != nil { | ||
return | ||
} | ||
containers := BuildSidecar(&targetStruct.chaosDetails) | ||
require.Equal(t, len(containers), len(targetStruct.chaosDetails.SideCar)) | ||
}) | ||
} | ||
|
||
func FuzzContains(f *testing.F) { | ||
f.Fuzz(func(t *testing.T, data []byte) { | ||
fuzzConsumer := fuzz.NewConsumer(data) | ||
targetStruct := &struct { | ||
val string | ||
slice []string | ||
}{} | ||
err := fuzzConsumer.GenerateStruct(targetStruct) | ||
if err != nil { | ||
return | ||
} | ||
contains := Contains(targetStruct.val, targetStruct.slice) | ||
for _, s := range targetStruct.slice { | ||
if s == targetStruct.val { | ||
require.True(t, contains) | ||
return | ||
} | ||
} | ||
require.False(t, contains) | ||
}) | ||
} | ||
|
||
func FuzzSubStringExistsInSlice(f *testing.F) { | ||
f.Fuzz(func(t *testing.T, data []byte) { | ||
fuzzConsumer := fuzz.NewConsumer(data) | ||
targetStruct := &struct { | ||
val string | ||
slice []string | ||
}{} | ||
err := fuzzConsumer.GenerateStruct(targetStruct) | ||
if err != nil { | ||
return | ||
} | ||
contains := SubStringExistsInSlice(targetStruct.val, targetStruct.slice) | ||
for _, s := range targetStruct.slice { | ||
if strings.Contains(s, targetStruct.val) { | ||
require.True(t, contains) | ||
return | ||
} | ||
} | ||
require.False(t, contains) | ||
}) | ||
} | ||
|
||
func FuzzGetRandomSequence(f *testing.F) { | ||
f.Add("random") | ||
|
||
f.Fuzz(func(t *testing.T, sequence string) { | ||
val := GetRandomSequence(sequence) | ||
if strings.ToLower(sequence) == "random" { | ||
require.Contains(t, []string{"serial", "parallel"}, val) | ||
return | ||
} | ||
require.Equal(t, sequence, val) | ||
}) | ||
} |
2 changes: 2 additions & 0 deletions
2
pkg/utils/common/testdata/fuzz/FuzzRandomInterval/711bbee1d16a50e2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
go test fuzz v1 | ||
string("2778") |